C++ Pointers-Concept Explained
C++ pointer or simply a pointer is a simple variable that has the capability to store the memory address of some location.
Lets discuss a small program for illustration:
Lets discuss a small program for illustration:
int * p; int q=10; p=&q; cout<<"Address of q is: "<
- Here p is a pointer variable and q is a simple interger variable.
- & is the address operator. So when we've the statement p=&q, we are actually initializing p with the memory address of q.
- As memory addresses are generally of hexadecimal type, so p now also has some hexadecimal value.
- p as such has no data type. But int *p means that the data type of the contents of memory location referenced by p (i.e. q) is interger.
- So p is also called pointer to integer.
- Displaying p will display the memory address of the variable q.
- *p displays the content of the memory location it refers to i.e. value of q.
Replies
-
Deepika BansalLets consider the following as well:
&*p is same as &(*p) which means address of the value stored.
*&*p is same as *(&(*p)) which means value of variable through pointer p.
*&p is same as *(&p) which means value of pointer.
This means we always resolve such expressions from the inwards and moving outwards. -
Deepika BansalPointer Expression
int x=5,y=5; int *p, *q; int a,b,c,d; p=&x; //p now contains the memory address of x ie p refers to x q=&y; //q now contains the memory address of y ie q refers to y a=(*p)+(*q); //x+y b=(*p)-(*q); //x-y c=(*p)*(*q); //x*y d=(*p)/(*q); //x/y *p=*p+10; // x=x+10
Here we are accessing the value of x and y using the pointers p and q as *p and *q respectively. -
Deepika BansalScale Factor
We can also have something like p++ or p--.
int *p; int x=10; p=&x; p++;
Here when we are incrementing or decrementing the pointer variable p is according to the data type of the variable it is pointing to i.e. x in our case.
In our example we are considering x to be of type int. Int type requires 2 bytes of memory location to store one integer value. So when we're having p++, we are actually moving two bytes so that p may now point to next integer value (which may be a garbage value as well).
Had p be of type char *, p would have been incremeted by 1 byte to point to next char.
Similarly for float *, p would have been incremented by 4 bytes to point to next float and so on.
So the key formula is:
ptr++ is same as ptr=ptr+1 which in turn is equivalent to ptr=ptr + (1*size of element).
PS: Division and Multiplication is not applicable this way.
You are reading an archived discussion.
Related Posts
C++ pointer or simply a pointer is a simple variable that has the capability to store the memory address of some location.
Lets discuss a small program for illustration:
int * p;
int...
Lets discuss a small program for illustration:
int * p;
int...
Hi all
I need your help, I've got two suppliers of Naval brass, the materials are the same from each supplier and a chemical analysis has confirmed this, a hardness...
Pulling off the ultimate career makeover - Fortune Management
www.natural-ui.com, ,Natural User Interface (NUI) is a Swedish innovative emerging technology company specializing in delivering commercially available advanced multi-touch software, hardware and service solutions, to the global market.
[video=youtube;oD-SacHOKqc]https://www.youtube.com/watch?v=oD-SacHOKqc[/video]
Do anybody know the placement procedure of HCL Technologies-server and storage services??
It would be great if you could give me the placement papers of that company.
I desperately need...