-2

Consider this code :

int main(){
   
   
   int a = 10;
   int* p = &a;

   //case 1

   *(&a);
   *p;

   //case 2

   *((&a) + 1);
   *( p + 1 );
   
   
}

  1. Are the two forms the same ?

  2. Is it correct to assert that a pointer is just an address that "can" be stored in a variable for later use ?

  3. Are there some caveats when not using a pointer variable to dereference an address ?

alessio solari
  • 313
  • 1
  • 6
  • #2... Turn it around just a bit: "A pointer is a variable that is used to store a memory address for later use." – Fe2O3 Jul 29 '23 at 10:05
  • Fe203, " If v is a variable of type T, then &v is a pionter with type T *". This is one of the comments I received when asking this : https://stackoverflow.com/q/76782387/14595895 – alessio solari Jul 29 '23 at 10:08
  • Fe203, so it turns out that if I have int a = 10; &a is indeed a pointer. This means that even they say a pointer is a variable that contains an address in the larger sense the address of an object is a pointer that refers to a certain type – alessio solari Jul 29 '23 at 10:10
  • The value of a variable (a "pointer") can be changed (re-assigned). The "address of" a variable cannot... You're free to think what you want... – Fe2O3 Jul 29 '23 at 10:14

2 Answers2

0

Yes, *p and *(&a) are the exact same. The variable p simply contains the memory address of a.

2. A pointer is simply a variable that contains a number that is interpreted as a memory address, most often a uint32_t for 32 bit systems and a uint64_t for 64 bit ones.

3. To my knowledge there aren't any. Both are just the same memory address.

Nicholas
  • 41
  • 9
  • Nicholas, but why do people keep saying that a pointer is a variable ? Should it be more correct to say that a pointer is just an address that refers to a certain type and that "can be stored" in a special kind of variable that holds a pointer. ? – alessio solari Jul 29 '23 at 09:34
  • You can change the value in a pointer, `p++`, therefore it is a variable. – stark Jul 29 '23 at 09:56
  • Also, "pointer arithmetic" changes the value of the variable (that is a pointer) by the appropriate number of bytes for the datatype that is being pointed at. – Fe2O3 Jul 29 '23 at 10:01
  • @alessiosolari A pointer is a variable, and in that variable is a memory address. When we dereference the pointer the compiler looks at that memory address and gives us the value found there. The type of the pointer just tells the compiler how to interpret the binary data at the memory address. – Nicholas Jul 29 '23 at 10:14
0

In case 1:

*(&a); de-references the address of a resulting in the value stored in a.

*p; de-references the value stored in p which is the address of a also resulting in the value stored in a.

So both have the same effect.

similarly, in case 2:

*((&a) + 1);
*( p + 1 );

both statements have the same effect but be careful here, a is declared as a variable of type int, not an array. As such, both statements have semantic error and result in undefined behavior.

machine_1
  • 4,266
  • 2
  • 21
  • 42
  • machine_1, but why do people keep saying that a pointer is a variable ? Should it be more correct to say that a pointer is just an address that refers to a certain type and that "can be stored" in a special kind of variable that holds a pointer ? – alessio solari Jul 29 '23 at 09:50
  • I mean the pointer in and of itself is just an address of a certain type... – alessio solari Jul 29 '23 at 09:51
  • A pointer is a special kind of variable that stores the address of another object. – machine_1 Jul 29 '23 at 09:51
  • machine_1, I've heard a lot of people saying that a pointer is just an address. If as you say it is a variable then why do we get the same result when dereferencing a raw address ? – alessio solari Jul 29 '23 at 09:55
  • @alessiosolari - `p` is a "variable", because its value can vary. If you do `p = &b;`, for example, it is no longer "just the address of a". – BoP Jul 29 '23 at 10:16
  • BoP, if &a is not a pointer then why if we do "pointer" arithmetic we get the same result as if we were to use a pointer variable. For example I can do (&a) + 1 and p +1 and I get the same result. The fact that I'm doing "pointer" arithmetic does imply that &a must be a pointer otherwise it would have been called "address arithmetic". Moreover it isn't just a variable pointer that has information about the type of object if points to, the &a address in and of itself has information about the type of object it points to because if we dereference *(&a) we get an int. – alessio solari Jul 29 '23 at 10:21
  • BoP, I've also asked this kind of questions on a previous post : stackoverflow.com/q/76782387/14595895 and it seems like there are opposite point of views as far as this matter is concerned. So what is the truth ? – alessio solari Jul 29 '23 at 10:24
  • From the IBM documentation "The & (address) operator yields a pointer to its operand. The operand must be an lvalue, a function designator, or a qualified name. It cannot be a bit field, nor can it have the storage class register." – alessio solari Jul 29 '23 at 14:51