pointer confusion

Hi,
I am having trouble with understanding pointer. Lets declare a pointer.

There is no difference between
 
  int* someIntPointerVar;


and

 
  int *someIntPointerVar;

, right?

Ok then lets initialize the pointer.

1
2
  int someOtherIntVar = 0;
  someIntPointerVar = &someOtherIntVar;


This is correct, right?

Then the main part where I am confused.

1
2
  int q = 0;
  q = *someIntPointerVar;


asterisk is not in declaration, so "*someIntPointerVar" means, "value stored at the address pointed by *someIntPointerVar pointer", right?

"someIntPointerVar" means the memory address itself to which the *someIntPointerVar is pointing, correct?

If so, then what does it mean by "&someIntPointerVar"?
*someIntPointerVar gives you the int value that someIntPointerVar is pointing to.

&someIntPointerVar gives you a pointer to the pointer someIntPointerVar.
pointer to the pointer??!!! I am not familiar with this idea. Can you elaborate a bit further?

Is it something like this:

I declared a pointer of type int. So compiler will temporarily allocate an address for this pointer until I initialize it or what.....I am getting no further....

* and & are complementary.


&myVar is the "address of" a variable so you can store it in a pointer;
int* myIntPointer = &myIntVar; // set pointer to be "address of" var.

*myPointer is the value that is pointed to rather than the pointer itself.
myIntVar = *myIntPointer; // sets int to be what the pointer is pointing at.

int age = 21;
int* pAge = &age;

int ageInDaysA = (*pAge) * 365;

*pAge = 25; // set age to 25
AMDA10 wrote:
I declared a pointer of type int.

Not sure if this is part of the misunderstanding or if you are just being imprecise. The type of a pointer can't be int because an int is not a pointer. The type of the pointer is actually pointer to int (int*).

AMDA10 wrote:
pointer to the pointer??!!! I am not familiar with this idea. Can you elaborate a bit further?

Pointer types are not as special as you might think. You can copy, pass them to functions and point to them using pointers the same way as with other types.

To get a pointer to a type you put an asterisk after the type name.

int* is a pointer to an int.
int** is a pointer to an int*.
int*** is a pointer to an int**.
...

As you can see it follows the same pattern.
Last edited on
pointer to the pointer??!!! I am not familiar with this idea. Can you elaborate a bit further?

A pointer is just a variable, like any other. The value it holds is interpreted as a memory address. If you can have a pointer to an int, or a pointer to a char, then you can also have a pointer to a pointer.

You can use it for all the reasons you'd use a pointer to any other type - e.g. for manipulating a C-style array of pointers, or for C-style passing a pointer by reference.

In C++, there's much less use for them - but then, in C++, there's much less use for pointers in general.
Last edited on
I think I am getting it.

"*someIntPointer" means a value stored at the location to which "someIntPointer" pointer is pointing.

"someIntPointer" means the value stored at the address of the pointer "*someIntPointer"

"&someIntPointer" means the address where "*someIntPointer" pointer itself is located. Note that this is different than that of the address of variable the pointer points to.

"**someIntPointer": here someIntPointer is pointing to another pointer, lets say "secondPointer". Secondpointer points to a variable int type "secondVar". So "**someIntPointer" means the value at secondVar.

The value of **someIntPointer is: someIntPointer [the value is a hex number which is address of "secondpointer"]
and its address is: &someIntPointer

A tutorial point article helped me much.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
     /* Possible ways to find value of variable num*/
     printf("\n Value of num is: %d", num); // output ## Value of num is: 123
     printf("\n Value of num using pr2 is: %d", *pr2); // output ## Value of num using pr2 is: 123
     printf("\n Value of num using pr1 is: %d", **pr1); // output ## Value of num using pr1 is: 123

     /*Possible ways to find address of num*/
     printf("\n Address of num is: %u", &num); // output ## Address of num is: XX771230
     printf("\n Address of num using pr2 is: %u", pr2); // output ## Address of num using pr2 is: XX771230
     printf("\n Address of num using pr1 is: %u", *pr1); // output ## Address of num using pr1 is: XX771230

     /*Find value of pointer*/
     printf("\n Value of Pointer pr2 is: %u", pr2); // output ## Value of Pointer pr2 is: XX771230
     printf("\n Value of Pointer pr2 using pr1 is: %u", *pr1); // output ## Value of Pointer pr2 using pr1 is: XX771230

     /*Ways to find address of pointer*/
     printf("\n Address of Pointer pr2 is:%u",&pr2); // output ## Address of Pointer pr2 is: 66X123X1
     printf("\n Address of Pointer pr2 using pr1 is:%u",*pr1); // output ## Address of Pointer pr2 using pr1 is: 66X123X1

     /*Double pointer value and address*/
     printf("\n Value of Pointer pr1 is:%u",pr1); // output ## Value of Pointer pr1 is:  66X123X1
     printf("\n Address of Pointer pr1 is:%u",&pr1); // output ## Address of Pointer pr1 is: XX661111
	 
	 
	 
	 
                  pr1             pr2                  num
    value:   66X123X1           XX771230               123
  address:   XX661111 	       66X123X1 	     XX771230
	 
	 










Last edited on
"someIntPointer" means the value stored at the address of the pointer "*someIntPointer"

Huh? That makes no sense. *someIntPointer isn't a pointer - it is, as you yourself said on your previous line, the value stored at the location that someIntPointer points to.
Topic archived. No new replies allowed.