A Pointer in C programming language is playing a key role. A pointer is very easy and fun to learn while learning the C programming. As you know, every variable memory location and also every location have some defined address, which can be accessed by using the ampersand (&) operator before the variable name.
A Pointer is variable which holds the address of another variable which direct address of memory location of a variable. See the below example of pointer:
1 2 3 4 5 6 7 8 9 10 11 12 | #include <stdio.h> int main () { int abc; char xyz[10]; printf("Address of var1 variable: %x\n", &abc ); printf("Address of var2 variable: %x\n", &xyz ); return 0; } |
The output of above program is:
1 2 | Address of abc variable: bff5b600 Address of xyz variable: bff5b3f6 |
You can define a pointer variable by using the (*) symbol before the variable defined. See the below how to define a pointer type variable in C programming language:
1 2 3 4 | int *ip; /* pointer to an integer */ double *dp; /* pointer to a double */ float *fp; /* pointer to a float */ char *ch /* pointer to a character */ |
Below is the simple example of pointer type variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int main () { int abc = 50; /* normal variable declaration */ int *xyz; /* pointer type variable declaration */ xyz = &abc; printf("Address of abc variable: %x\n", &abc ); /* address stored in pointer variable */ printf("Address stored in xyz variable: %x\n", xyz ); /* access the value using the pointer */ printf("Value of *xyz variable: %d\n", *xyz ); return 0; } |
The output of Above program is:
1 2 3 | Address of abc variable: 9b62b724 Address stored in xyz variable: 9b62b724 Value of *xyz variable: 50 |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c pointers, pointer in c, pointer on c, ponters in c, what are pointers in c