Home > Software engineering >  Why don't pointers have same address as the variable they are pointing to?
Why don't pointers have same address as the variable they are pointing to?

Time:10-12

I am a beginner at C (and have no knowledge of C , coming form a Java and Python background).
I was learning about pointers and ran the following code I am sharing from my self tutorial file:-

  #include<iostream>
   using namespace std;

    int main(){
        //What is a pointer:-
        // A datatype that holds address of a variable in other datatype
        
        int a=3;
        int* b = &a; //CREATE A POINTER THAT POINTS TO A
                     // '&'STANDS FOR ADDRESS OF OPERATOR
                     // '*' IS CALLED DEREFERNCING OPERATOR
        cout<< b<<endl; //prints address of a
        cout<<&a<<endl; //does the same stuff
        //retreving stored at a particular address
        // '*' GIVES VALUE AT THE ADDRESS STORED IN POINTER
        cout<<*b<<endl;
        cout<<*&a<<endl;
        //pointer to a pointer
        int** c = &b; // pointer to another pointer b
        cout<<"The address of b is"<<c<<endl;
        cout<<"The address of b is"<<&b<<endl;
        cout<<"The value at address c is"<<*c<<endl;
        cout<<"The value at address value stored in c is "<< **c<<endl;
        return 0;
    }

Which returned me the following output:-

0x94ac7ff7d4
0x94ac7ff7d4
3
3
The address of b is 0x94ac7ff7c8
The address of b is 0x94ac7ff7c8
The value at address c is 0x94ac7ff7d4
The value at address value stored in c is 3

What sparked curiosity were the last four lines of the output we see that:-
c points to b
and
b points to a

They are not variables themselves then why don't they return the same address? Does that mean using multiple pointers for same variable can take up system resources thus resulting in bad design?

CodePudding user response:

A picture is worth a thousand words.

    ---------------- 
a: |              3 |               94ac7ff7d4
    ---------------- 
    ^                              
    |
    `-------------.
                  |
    --------------|- 
b: | 0x94ac7ff7d4 * |               94ac7ff7d8
    ---------------- 
    ^
    |
    `-------------.
                  |
    --------------|- 
c: | 0x94ac7ff7d8 * |               94ac7ff7dc
    ---------------- 

I think of the variable a as a little box that can hold an integer. In C we know it by the identifier a, but actually, at the machine level the compiler has assigned it to sit at address 0x94ac7ff7d4.

Similarly, b is a little box that can hold a pointer to an integer, which is typically implemented as the address (a number) where the integer is stored. We know it by the identifier b, but actually, the compiler has assigned it to sit at address 0x94ac7ff7d8.

Finally, c is a box that can hold a pointer to a pointer to an integer, which is again implemented as the address where the pointer is stored. We know it by the identifier c, and although you didn't say so, I'm guessing that the compiler has assigned it to sit at address 0x94ac7ff7dc.

For any variable, & gives you the address where the variable is stored — which is the sort of value you can store in a pointer variable.

For a pointer value, * gives you the value that the pointer points to. (And for pointers to pointers, ** gives you the value that the pointer that the pointer points to, points to, etc.)

CodePudding user response:

Keep in mind C is a very low level language. Many things exist literally without fancy handling.

int *b;

b is a variable just like any value. It exists at address 0x94ac7ff7c8 and contains the value 0x94ac7ff7d4;

int **c = &b;

c is also a variable. it exists in memory (at an address not evaluated by the program) it contais the value 0x94ac7ff7c8, which is the same value as the address of b.

Unlike php's references, C's pointers will not automatically point to the 'real' variable. when doing this all you're effectively doing is assigning a value to c, because that's what the = operator does.

c = 0x94ac7ff7c8;

hence, c and b contain different values.

When evaluation '**c' you're asking:

"What's the value held at address c? 0x94ac7ff7c8. Okay, then What's the value held at the address 0x94ac7ff7c8 ?" the answer to that question is **c

Note that the following lines have two different meanings:

int **c = x;
**c = x;

In the first, you're declaring a double pointer type. The asterisks belong to the expression int **. Here x must be of type int **

In the second, you're dereferencing c twice. The asterisks belong to the expression **c as in *(*(c)). Here x must be of type int

The distinction does not pose ambiguity because it is impossible to dereference a pointer when declaring it. It has no value, so no address to fetch.

CodePudding user response:

If you just write &a then it is not a variable, but as soon as you declared an integer, it becomes the variable by the name b which happens to store the address of another variable.

CodePudding user response:

Variables declared as pointers are objects of pointer types. They occupy their own memory.

So these declarations

int a=3;
int* b = &a;
int** c = &b;

declare three different variables that store different values. The first variable has the type int and stores the value 3. The second variable has the type int * and stores the address of the first variable. The third variable has the type int ** and stores the address of the second variable.

If you have a declaration like

T a;

where T is some type specifier then a declaration of a pointer to this variable will look like

T *p = &a;

where T for example can be int *. In this case you will have the declaration

int * *p = &a;

or omitting redundant blanks it looks like

int **p = &a;

CodePudding user response:

A pointer is an object like others. It has a type and a value. Typically a pointer holds the address of another object, though that does not make them as special as you think.

Consider this:

int a = 0;
int b = 0;
int* p = &a;
p = &b;

int* p = &a; initializes p to point to a. ps value is the address of a. The next line makes p point to b. Now ps value is the address of b. The pointers value needs to be stored somewhere, just like with int a = 0; the value 0 needs to be stored somewhere.

In memory the values of the int a and of int* p are just bits and bytes. It is only the interpretation as either "an integer" or "points to an integer" that makes pointers special.

CodePudding user response:

Why don't pointers have same address as the variable they are pointing to?

A pointer is a distinct object from the object that the pointer points to (unless the pointer points at itself). Two distinct objects that have overlapping storage duration cannot typically be stored in the same address.

They are not variables themselves

This is an incorrect assumption. b and c are variables themselves.

Does that mean using multiple pointers for same variable can take up system resources

Most objects can potentially consume memory. Pointer objects are no exception to that.

thus resulting in bad design?

There is no way to make such conclusion from the premise. Sometimes many pointers are beneficial.

CodePudding user response:

List item

A Variable is a memory location in a computer's primary memory(RAM) which is used to store data. A pointer is a memory location in a computer's primary memory(RAM) which is used to store the address of a variable. pointers do not have the same address as the variable they are pointing, because pointers are also a type of variable, which has a different and own addresses.

CodePudding user response:

Consider the following code in light of your "not a variable" impression.

int a = 3;
int b = 4;
int *p = &a;
std::cout << p << " -> " *p << std::endl;
p = &b;
std::cout << p << " -> " *p << std::endl;
  • Related