i am using c++ and i have some trouble about pointers
i know that if we declare some variable
int t=7;
int *p=&t;
*p gives adress of variable t but in c++ here also definition of **s why it is used ?i have read it but i have some misunderstanding please can anybody give me example how use?let take about given example
First of all *p will give you the value of variable t and not the address of t. Only ‘p’ will give you the address of t.
Essentially every address is just an integer number. If you are compiling for 32-bit architecture then it will be 32-bit long number and if you are compiling for 64-bit then it will be 64-bit long. This means that you can use a simple integer variable to store any variable’s address irrespective of what type the variable is but it is better to use a proper type of pointer to store the variable’s address. This way we can dereference the pointer and we dont have have to type cast it whenever we want to use the variable which is being pointed to.
To create a pointer to some variable, we declare a variable of the same type but put a ‘*’ in front of the variable’s name. For example you can declare an integer as follows
To create a pointer to ‘a’, create a new variable with the same type as a and just put a ‘*’ in front of it. So the declaration of pointer to the above variable ‘a’ will look like
Notice that other than an additional ‘*’, its the same declaration. Now ‘p’ is a pointer to an integer variable.
Now consider that you want another pointer to point to this variable ‘p’. Again you declare a variable similar to ‘p’ and just put a ‘*’ in front of it. So a declaration of pointer to the above variable will look some thing like
Again notice that other than an extra ‘*’ this declaration is very same to the declaration of ‘p’. So the variable ‘pp’ is a pointer to a pointer to an integer
You can declare a pointer to ‘pp’ in the same way and the cycle will continue. This is also true for non simple types i.e pointers to variables of class or structure types are created the same way.