Pointer is an important concept in C programming Language, you should have a very good understanding of pointers. For most people, it will take some time to fully understand pointers. So be patient. You have to learn pointers because they are used everywhere in the C language.
So why use pointers? Why don’t we use arrays to create data structures?
With an array, you have to declare its maximum size at the beginning. But with a pointer memory allocated at runtime.There is a case, in which you have to use 10 megabytes initially and later 15 and after that 20, If you create an array for this with 20 megabytes size, remaining 10 Mb storage will be wasted. With pointers, you can create dynamic data structures. Instead of claiming the memory up-front, the memory is allocated while the program is running. So the exact amount of memory is claimed and there is no waste.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.
To declare a pointer you have to put an asterisk * in front of its name. A pointer can be typed or untyped. (A typed pointer points to a particular variable type such as an integer. An untyped pointer points to any data type).
int *ptr_A; // typed pointer
void *ptr_A; // untyped pointer
There are few important operations, which we will do with the help of pointers very frequently.
- we define a pointer variable
- assign the address of a variable to a pointer
- finally, access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. Following example makes use of these operations:
int main ()
int normal_var = 100; /* actual variable declaration */
int *ptr; /* pointer variable declaration */
ptr= &normal_var; /* store address of var in pointer variable*/
printf("Address of normal_var variable: %x\n", &normal_var);
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ptr);
/* access the value using the pointer */
printf("Value of *ptr variable: %d\n", *ptr);
Address of normal_var variable: accfkk
Address stored in ptrvariable: accfkk
Value of *ptrvariable: 100
Pointer is an important concept, so you have to spend more time with it, and need to study clearly, in this we are giving you only basics in the pointer.we kindly advice you study pointers in detail with the help of the other reference If time and your positive reviews come to our hand, we will give separate tutorial for pointer.Best of Luck.