Home >>C Tutorial >Pointers in C

Pointers in C

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 ;

Declaring A Pointer.

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  

Pointer Example

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;  
}     
Output :
Print the Address of p variable = c3860f54
Print Value of p variable is =10

Pointer to array declaration

int array[5];  
int *p[10]=&arr ;

Pointer to function declaration

void disp(int);  
void(*p)(int) = &disp ;

Pointer to sturcture declaration

struct student {  
    int roll;  
    float fees;  
}ref;  
struct student *p = &ref ; 

The Advantage Of The Pointers In The C Language

  • Pointers are used to retrieving, trees, strings, etc. and used with arrays, structures, and the functions and also pointer in the C language reduces the code and enhances the performance,
  • Multiple values can be returned by the end user from a function just by using the pointers in the C language.
  • The pointers in the C language allows the user to access any memory location in that is located in the computer's memory.

The Usage Of The Pointer In The C Language

Pointers in the C language have known to be of multiple uses, some of them are as follows:

  1. 1. Dynamic memory allocation :Memory can be allocated dynamically in the C language just by using malloc() and calloc() functions and that is where the pointer is used.
  2. 2. Arrays, Structures, and Functions : The Pointers in the C language are known to be most widely used in arrays, functions, and structures, the main reason of its usage is that it generally reduces the code and enhance the existing performance.

Address Of The (&) Operator

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;  
}    
Output :
value of num = 10
address of number =1367034380

Null Pointer In The C Language

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.

Let's Understand The Process Of Reading Complex Pointers.

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:

  • *: This operator is the C language is generally known as the pointer operator.
  • ( ): In order to declare and define the function, this bracket operator is used.
  • [ ]: The depicted operator is known as an array subscript operator
  • Data type : It is basically the type of the variable towards which the pointer is intended to point and this variable includes the modifier like signed int, long, etc).
  • Identifier : This operator has the privilege to get assigned on priority. Identifier is basically the name of the pointer.

Here is the process to read the pointer: int (*p)[10].

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:

  • char -> 4
  • [10] -> 3
  • -> 2
  • p -> 1

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.


No Sidebar ads