Pointers in C, Dereferencing Pointer, Pointer Variable, Example
C pointer
Pointer in C programming is a special variable that can store the address of another variable. Normal variables hold the value of a certain type whereas the pointer variables can hold the address of a variable or any other address.
Pointers are extremely important in C.
- They are essential particularly when using data structures like strings or arrays or linked lists.
- Pointers are useful for dynamic memory allocation. When you declare a regular variable in C, memory address for the variable is decided automatically.
- When you declare pointers in C, you can manually allocate memory, decide on when to allocate memory and which memory address to allocate.
Pointer Declaration
As with any other type of variable, the pointer variable must be declared before we can use it in our programs. Let us look at the declaration of normal variable.
int n; float num;
To declare the pointer variable, you simply insert * in front of the variable name.
int *n; /* pointer to int n */ float *num /* pointer to float num */
Therefore, the syntax of pointer declaration is given below
Syntax: data-type *variable-name;
Here data-type must be any of the valid C data types and the * tells the compiler that this variable is a pointer variable.
Pointer - Address Operator and Dereferencing Pointers
There are two operators that work with pointer
- & ----- address of operator
- * ----- value at address
The & (address of ) operator
The & (ampersand ) operator is used to assign the address of one variable to a pointer
For example: int a = 20; /* to define normal variable and initialized with 20 */ int *num; /* to define pointer to int num variable */ num = &a; /* inintialize with address of variable a to pointer*/
If you don't assign an address of a variable to a pointer, the pointer is uninitialized and you can't use it for anything.
Dereferencing Pointer
The pointer is declared with the " * " symbol. This operator indicates that declared variable is a pointer variable rather than normal variable.
For example int *num; float *price; char *p;
This is also known as dereferencing ( * ) operator. By using this * operator, we can access the value of a variable through a pointer.
For example int A=10, *num; num=&A; printf("%d", A); / * it prints " A " value that is 10 * / printf("%d", *num); /* this is also print " A " value 10 through pointer " num " */
Both the above printf statements will return the same result. The 1st statement is the regular printf statement. In the 2nd statement, * dereferencing operator fetches the address stored in num variable and then gets the value stored in the fetched address.
Note:
- If * is used in the declaration, then * is a pointer-type variable and occurs only in variable or function declarations.
- if * is used in all other circumstances, then * means dereference or access the pointed-to object.
NULL Pointer
To avoid a pointer from pointing to an arbitrary address, it is a good practice to assign NULL value to a pointer variable. The pointer assigned with NULL is known as the NULL pointer. We can assign the NULL to the pointer at the time of pointer declaration.
For example: int *pt = NULL;
C pointer Example Program
#include<stdio.h> void main() { int a = 50; int *ptr; ptr = &a; printf("\n The value of a is :%d ",a); printf("\n The value of pointer ptr is :%u ",ptr); printf("\n Access value of "a" through pointer is :%d ",*ptr); }
Output:
The value of a is : 50 The value of pointer ptr is : 64868 Access value of "a" through pointer is : 50
Summary - Pointers in C
- Pointer stores address of another variable.
- Use * to define the pointer variable and dereference the pointer variable.
- Use & to get the address of a variable.
- Do not make pointer variable of one data type to point to a variable of another data type.
strcat() - String Concatenation strcmp() - String Compare
strcpy() - String Copy strlen() - String Length