PREVIOUS NEXT
PREVIOUS NEXT
Pointer in C Language
Pointer is one of an important feature of c language.
This is the most powerful feature of c language. Pointer is a variable same as
other variable but with a little different. The variable we declared except
pointer variable holds the value, but a pointer variable holds the address of a
variable of same pointer declaration type.
The declaration of a pointer type variable
int*p;
The declaration tells - p is a pointer variable
of integer type which holds only the address of an integer type variable.
Like this the declaration of a character, float type
variable is
char*p;
float*p;
Let us see with this example
The declaration of a simple integer type variable is
int var = 233;
Var
233
|
1635
When the control execute this statement, it allocates 2
bytes of memory for this variable let the address of the first byte is 1635and
stores integer constant 233 in this 2 bytes of memory and name the location of
the memory as var. When we are going to access the value of the variable
there is no use of the address of the variable. The simple variable may be a
signed variable or may be an unsigned variable.
int * p;
int var=233;
p=&var;
var
233
|
1635
p
1635
|
3856
When the control execute, it allocates 2 bytes of memory
for pointer variable let the address 3856 and 2 bytes of memory for
general variable let the address 1635 and stores integer constant 233 in this 2
bytes of memory and stores the address of the general variable 1635 in the
pointer variable. The pointer variable always stores unsigned integer type
value because the address of any variable is unsigned integer type..
Let us see an example
main()
{
int * p;
int var=233;
p=&var; // assigning address to the pointer
printf("%d",var);
printf("%u",p);
printf("%d",*p);
}
The output is 233 1635 233
When we are accessing a pointer variable like any other
normal variable like in second printf statement it gives the value stored
in the variable p i.e 1635. If we want to access the value stored at the memory
location 1635 then we have to use *p not only p like in third printf.
How to read a pointer variable
1. int *p;
This can be read as - p is a pointer variable which can hold the address
of an integer variable.
2. char *p;
This can be read as - p is a pointer variable which can hold the address
of a character variable.
3. float *p;
This can be read as
- p is a pointer variable which can hold the address of a float variable.
4. double *p;
This can be read as
- p is a pointer variable which can hold the address of a double variable.
5. int**p;
This can be
read as - p is a pointer variable which can hold the address of another pointer
variable which hold the address of an integer variablePREVIOUS NEXT
No comments:
Post a Comment