Showing posts with label Programming in C. Show all posts
Showing posts with label Programming in C. Show all posts

Sunday, 29 May 2011

Difference between pointer variable and simple variable


A variable has 3 things when you make them :
int <- type
x <- name
=3 <- data

A pointer doesn't have any data in themself, they "point" to a variable.
Like this :

int x = 3;
int *x = x


now the *x is 3, because it "points" to the x variable ( actually it points to it's memory address )

*x = 6 means that where the pointer is pointing ( the memory part ) will be filled with this data.


a little more thing:

incrementing ( i++ ) a pointer can do two different things :
(*x) ++ adds +1 to the value , while
*x++ will move the "pointing" to the next thing in the memory ( if we are not talking about an array, then it's usually memory garbage )
**************************************************************************************


normal variable stores a value of the given datatype where
as the pointer variable stores the address of a variable.

for example int n=10;
int *p;
p=&n;
here p is a pointer variable and n is a normal
variable.p stores the address of n where as n stores an
integer value.
*p prints the value of n,p prints the address
of n.