Home > database >  What is the address of a pointer in C?
What is the address of a pointer in C?

Time:11-24

    int main()
    {
    int *p;

    printf("%p \n", &p);
    printf("%p \n", p);

    return 0;
    }

By executing this code I am receiving the following output:

    0x16b77f710 
    0x104683f4c 

I expected to get the same memory address, because the &p id not referenced to any other variable.

Why i am getting two different memory address?

Thanks,

CodePudding user response:

The pointer is a normal object having (in your case type of int *). It cant point to itself because the type of such pointer would have to be int **

*image stolen from internet.

CodePudding user response:

A pointer is a variable like any other. It has an address, which is typically the address in memory where that variable sits.

Like any other variable, a pointer variable also has some data in it. For a pointer variable, that data is the address of some other variable, the variable at which the pointer points.

The address of a variable, and the contents of a variable, are two totally different things. They are almost never equal.

Try this program, in which I give your variable p something to point to:

int main()
{
    int i = 5;
    int *p = &i;

    printf("p: %p, p's address: %p\n", p, &p);
    printf("i: %d, i's address: %p\n", i, &i);
}

You should notice two things:

  1. As in your first program, "p" and "p's address" will be different.
  2. Whatever value you see for "p", it will be the same as "i's address".

CodePudding user response:

The reason is that a pointer, when declared, does not point to itself by default. Simplified you can imagine it in such a way that a pointer occupies 2 memory cells. 1 memory cell has the virtual address where the pointer itself is located (&p in your case), the 2nd memory cell contains the virtual address where the pointer points to (p in your case).

Since memory cells retain their value when they are deallocated, the cell containing the destination address of your pointer still contains an obsolete value from another, already completed process.

You would have the same phenomenon if you declare a new integer variable and then print its value with printf, you will see that there will already be some number in the new variable that will appear completely random. This is also due to the fact that there is still an obsolete value in the corresponding memory cell.

CodePudding user response:

In simple terms, you have declared a variable:

int* p;

This is a pointer to an integer. Thus you can display the value of this pointer address.

But, you need to point it at something. You never did that. As others have said the result will be undefined with respects to the value.

Keep in mind:

  1. Pointer variable has its own address like any variable.
  2. Pointer value depends on what you get the pointer to point to.

CodePudding user response:

Let assume there is random memory block for just understanding name it a. Now assume that p is pointing to that memory block.

&p returns the address of memory block where p is present.

p returns the address of memory block(&a) to the variable/memory block(a) which p is pointing.

So of course it will give different memory addresses.

  • Related