What are Pointers in C?
The pointers perform the function of storing the addresses of other variables in the program. These variables could be of any type- char, int, function, array, or other pointers. The pointer sizes depend on their architecture. But, keep in mind that the size of a pointer in the 32-bit architecture is 2 bytes.
Let us look at an example where we define a pointer storing an integer’s address in a program.
int x = 10;
int* p = &x;
Here, the variable p is of pointer type, and it is pointing towards the address of the x variable, which is of the integer type.
How Do We Use Pointers in C?
Consider that we are declaring a variable “a” of int type, and it will store a value of zero.
int a = 0
Now, a is equal to zero.
Declaration of a Pointer
Just like the variables, we also have to declare the pointers in C before we use them in any program. We can name these pointers anything that we like, as long as they abide by the naming rules of the C language. Normally, the declaration of a pointer would take a form like this: data_type * name_of_pointer_variable;
Note that here,
The data_type refers to this pointer’s base type in the variable of C. It indicates which type of variable is the pointer pointing to in the code.
The asterisk ( * ) is used to declare a pointer. It is an indirection operator, and it is the same asterisk that we use in multiplication.
We can declare pointers in the C language with the use of the asterisk symbol ( * ). It is also called the indirection pointer, as we use it for dereferencing any pointer. Here is how we use it:
int *q; // a pointer q for int data type
char *x; // a pointer x for char data type