Why should I use a double pointer variable to get another pointer address (& ptr1)

int num = 45,*ptr1,*ptr2; ptr1=# ptr2=&ptr1; printf("%d\n",*ptr1); 

I thought about this question for a while, but could not find a way to understand why &ptr1 cannot be assigned ptr2 in line 3, &ptr1 is the address of the pointer, this address is no different from another address, such as the address of an integer, say

 int a=1; ptr2=&a; 

What does it mean that I can assign an integer address to a pointer, but not the address of a pointer to a pointer, what differences between these two "addresses" can make them different? The address of shared variables can be assigned to one pointer, but the address of pointers cannot be assigned to one pointer?

I know the right way to do this is to use a double pointer to declare ptr2 , but why can't a single pointer?

+6
source share
7 answers

Simply put, pointers are not addresses; they are variables representing an address with a type. Thus, types are compatible for pointers to assign (except for the void * generic pointer).

 ptr2 = &ptr1; 

ptr1 is of type int * , therefore &ptr1 is of type int ** , it does not match ptr2 , which is of type int * .

Ref: C99 6.5.16.1 Simple Purpose

both operands are pointers to qualified or unskilled versions of compatible types, and the type that the left points to has all the qualifiers of the type that it points to from the right.

+3
source

Yes, you can assign a pointer address to a pointer, but it must be a pointer to a pointer variable.

 int **ptr3; ptr3 = &ptr1; 

The reason you cannot assign it the way you tried is because the pointer to int does not match int. Pointers must indicate compatibility of the same type. If you really know what you are doing, you can obviously give it up, but this is the path to danger.

+1
source

Your code is incorrect. This expression:

 ptr2 = &ptr1; 

Trying to make int * from int ** without a cast. Standard C prohibits such conversions without explicit casts.

The reason this is not allowed is because pointer types are not guaranteed by the standard for all of the same size - so the pointer to your pointer may not match the variable that you specified as a pointer to int .

Since pointers to any type can be implicitly converted to and from void * , you could write (correct, but probably confusing) similar code, in what in your question:

 int num = 45; void *ptr1, *ptr2; ptr1 = # ptr2 = &ptr1; 

But at the same time, you will need to transfer all type information in some other way:

 printf("%d\n",*(int *)ptr1); printf("%d\n",*(int **)ptr2); 
+1
source

The short answer is that type matters; a pointer to an int is another, incompatible type from a pointer to a pointer to an int . As others have noted, different types of pointers can have different sizes and views.

A pointer value is not just an address; it has additional type semantics. For example, the ptr++ expression will advance a pointer to the address of the next object of the base type. If the base type is char , then the pointer has an advanced 1 byte. If the base type is int , the pointer advances sizeof (int) bytes.

+1
source

Simply put, because it confuses the compiler. The compiler can only work in accordance with the language standard. He does not have his own brain.

The language standard tells the compiler that if there is int *

go to the address stored in this variable and use it.

In case there is int ** , then he tells him

go to the address in this variable. You are not finished yet, as it is also an address. Go there and use what is there.

This goes on and on for int *** etc.

Hope this helps you overcome this basic confusion.

+1
source

If you can assign an address to any pointer, regardless of its type, on the grounds that one address is similar to any other address, consider the problem that you might get into if the following becomes legal:

 int n = 40; int * p = &n; int ** pp = &n; /* Typeless address assignment as you would like */ printf("%d\n", **pp); /* Bad Things happen here */ 

or vice versa:

 int n = 40; int * p = &n; int * p2 = &p; /* More typeless address assignment */ printf("%d\n", *p2); /* Definitely not what you want */ 

Even if one address was the same as any other, a reasonable dereferencing of the pointer would be somewhat unpleasant if everything worked as you suggest.

The reason you cannot do what you offer is because the type information that you lose as part of your offer is necessary for dereferencing to work. If all you wanted the pointers to do was store and receive addresses, you will have a period, but they are not just used for that. If they were, we could have void pointers and do with them.

0
source

I completely agree with your statement that when a pointer variable will always store an integer value, since the address of any variable / array will be an integer.

But still, the data type that is used to declare the pointer is the address that it will store.

There are 3 points:

  1. The bits that are used while storing integer values differ from machine to machine. ie 32-bit, 64-bit and further more complications may add-up. 2. Memory occupied ie bytes of data stored in it. Reason is : somewhere even the pointer variable is stored in memory. Right? 3. There are certain operations associated with pointers like ++ or --. 

Remember that the type of pointer depends on the type of variable that it points to. This is the reason / need for a pointer to a pointer.

0
source

Source: https://habr.com/ru/post/949570/


All Articles