Home >>C Tutorial >Pointers in C
The pointers in C language are basically a variable that is known to store the address of another variable and the variable can be from these mentioned type, array, function, int, char or any other pointer. The architecture is responsible for the size of the pointer but in 32-bit architecture the size of a pointer is just 2 byte.
Here is an example for the better understanding of the subject.
In the following example, a pointer is defined that stores the address of an integer:
int num = 10; int* p = &num ;
The declaration of the pointer in the C language is generally done by the using *(asterisk) symbol. In other words, it can be understood in a way that it is used to dereference a pointer also called as indirection pointer.
Here is the syntax of the same:
int *a;//pointer to int char *c;//pointer to char
Print the address and value of a variable using Pointer
Let's see the pointer example as explained for the above figure.
#include<stdio.h> int main(){ int num=10; int *p; p=&num ; printf("Print the Address of p variable = %x \n",p); printf("Print Value of p variable = %d \n",*p); return 0; }
int array[5]; int *p[10]=&arr ;
void disp(int); void(*p)(int) = &disp ;
struct student { int roll; float fees; }ref; struct student *p = &ref ;
Pointers in the C language have known to be of multiple uses, some of them are as follows:
In order to display the display the address of a variable, users need to use %u as the address of the (&) variable is known only to return the address of the variable.
#include<stdio.h> int main() { int num=10; printf("value of num = %d , address of num = %u",num,&num); return 0; }
Null pointer in the C language is the pointer that has no value assigned to it, the only value it has got is NULL. NULL value can be assigned in the case where the user don’t have any address that can be specified in the pointer while declaration and it is known to deliver a fantastic strategy.
Here is the syntax of the null pointer in the C language:
int *p=NULL;
Note : The value of the pointer is zero in majority of the libraries.
While reading the complex pointers in the C languages, there are certain things that are to be kept in to consideration. Here are the following precedence and associativity of the operators that are used regarding pointers:
Operator | Precedence | Associativity |
---|---|---|
Data type | 3 | |
*, identifier | 2 | Right to left |
( ), [ ] | 1 | Left to right |
In the above mentioned table, the things to be noticed are as follows:
In order to read the pointer, the user must notice that () and [] generally have the equal precedence. Hence, the associativity of these pointers must be considered by the user. In terms of priority, () have all the priority as the associativity is generally seen to be from left to right.
The point to be noted here is that pointer operator * and pointer name (identifier) p generally possess the same precedence when they are inside the bracket (). Hence, the priority privilege will go to p and the second most privilege to priority will go to *, as their associativity that is to be considered here that goes from right to left.
The data type in the pointers are known to have the last precedence hence, the third most privilege of priority will be assigned to []. Considering all the above mentioned points the precedence will be as follows:
Now applying the above mentioned points, reading of the pointer will be as p is a pointer to an array of integers that have the size 10.