Pointers Declaration, Initialisation and accessing :
Declaration of Pointers
Syntax:
Type *pointer-variable-name;
Example:
int *p;
float *q;
char *r;
In the above declaration a pointer to integer p, pointer to floating point q and pointer to character r is created.
Initialisation :
After the declaration or at the time of declaration we have to initialize the pointer so that it will point to some variable. Later we can access the value of variable whose address is stored in pointer can be accessed through pointer.
int a=10;
Method 1:
int *p=&a;
Method 2:
int *p;
p=&a;
In both the above methods address of variable 'a' is stored into a pointer p. Now pointer p can access the value of variable 'a'.
Accessing pointers :
To print/use the value of variable pointed by pointer can be accessed by writing *p.
Declaration of Pointers
Syntax:
Type *pointer-variable-name;
Example:
int *p;
float *q;
char *r;
In the above declaration a pointer to integer p, pointer to floating point q and pointer to character r is created.
Initialisation :
After the declaration or at the time of declaration we have to initialize the pointer so that it will point to some variable. Later we can access the value of variable whose address is stored in pointer can be accessed through pointer.
int a=10;
Method 1:
int *p=&a;
Method 2:
int *p;
p=&a;
In both the above methods address of variable 'a' is stored into a pointer p. Now pointer p can access the value of variable 'a'.
Accessing pointers :
To print/use the value of variable pointed by pointer can be accessed by writing *p.
Comments
Post a Comment