Pointers in C

Introduction

Although, the C language and its concepts are very simple and easy to understand. Sometimes ‘Pointer in C’ is considered as its toughest module by some of the programmers but this topic is interesting as well. Just pay a little more attention to this chapter to have a strong grip on it.

Now come to the basic portion of our C language that we had started in our starting chapters i.e., ‘declaration of a variable’.

Pointers in C

Hopefully, you all are aware of the declaration and its representation is shown above.

Pointers in C

As we all know when we declare a variable (say ‘a’) it is allocated with some memory location and the value assigned to that variable gets stored in that memory location. Now, what does this ‘memory location’ means? Like we all have a particular location say home, office, school, etc. We identify these locations by their proper names, landmarks, etc. that is known as ‘address’. So, in the world of programming, if any variable is located at a memory location, then it must have its address. Moreover, the address is of that memory location in which the value of ‘a’ is stored. Relate a real-life situation to get a better understanding.

A boy named ‘john’ is getting ready for school and having his breakfast at his home located at ‘XYZ’ location. So now the location of john is XYZ. After an hour, he is attending his first lecture in the school located at some location ‘PBHY’, so now the current location of john is PBHY. Thus, ‘XYZ’ and ‘PBHY’ are the address of the home, and school respectively not the address of john, he is the one who is occupying those locations for some point in time. Similarly, any variable stored at a memory location has a particular address but that address is of that location not of that variable.

The address of ‘a’ is an integer like 562897113. It is important to note that the value of address varies for every computer as per the memory allocated to ‘a’ at that time.

memory allocation in C

Definition of Pointers in C

A Pointer in C is something that points to a variable and stores the address of that variable in it.

For example, ‘a’ has an address of 562897113, then the pointer to variable ‘a’ will store the value 562897113 in it. Consider ‘b’ is a pointer to ‘a’ thus ‘b’ will point to ‘a’ and the value of ‘b’ is 562897113. Moreover, the pointer ‘b’ has some address but that will be something different.

Understanding Pointer in C

&a’ is the representation of ‘address of a’. We all are familiar with ‘&a’ because we use it in ‘scanf( )’ while taking inputs from the user. We were taking input from the user and storing it at the address of the variable written with ‘&’ symbol.

How to use pointers

#include<stdio.h>

int main()

{

int x;

printf("Enter any value:\n");

scanf("%d",&x);

int *p;

p=&x;

printf("Address value of %d is %d", x, p);

return 0;

}

 

Output:

Pointer Output

Explanation

int *p; this declaration statement declares that ‘*p’ is an integer. But ‘*p’ is not a normal variable like ‘x’ because it is written with ‘*’. So, this means ‘p’ points to an integer value or ‘p’ is containing the address value of integer ‘x’ because it is a pointer to the integer.

Thus, ‘p’ is a pointer to ‘x’.

Note: ‘*p’ is not a pointer, ‘p’ is a pointer. ‘*’ is just used to differentiate it from other variables.

p=&x; since ‘p’ stores address value of ‘x’. ‘&x’ represents the address of ‘x’ which is assigned to ‘p’ and ‘p’ is already declared as a pointer. Thus, this declaration means that ‘p’ will now store the address of ‘x’.

printf(“Address value of %d is %d”, x, p); this statement prints the value of ‘x’ and ‘p’ on the output window. Thus, we came to know about the address of memory location where 56 is stored. Note that in this declaration statement we use ‘p’ only to access the value of address not ‘*p’. As already said, ‘p’ is the pointer not ‘*p’.

Then what does ‘*p’ means?

*p’ is an integer that has the value to which ‘p’ is pointing. Now, ‘p’ is pointing to ‘x’ therefore, ‘*p’ contains the value of ‘x’ i.e., ‘*p’ is 56.

Pointer Explanation

Example

#include<stdio.h>

int main()

{           

             int x=100;

              int *p;

            p=&x;

            printf("x=%d\n",x);

            printf("p=%d\n",p);

            printf("*p=%d\n",*p);

            printf("&p=%d\n",&p);

            printf("*&p=%d\n",*&p);

            return 0;

}

Output:

Pointer Output 2

Explanation

Clearly, ‘p’ is a pointer to ‘x’ so ‘p’ stores the address value of ‘x’ thus, ‘p=6487580’.

‘*p’ will contain that value to which ‘p’ points that’s why ‘*p=100’ since ‘p’ points to ‘x’ and x is equal to 100.

‘p’ contains the address of ‘x’ but has its address also i.e., ‘&p=6487568’.

Now, ‘*&p’ will contain that value to which ‘&p’ points and ‘&p’ points to ‘p’ thus, ‘*&p=6487580’.

Note: every time you run the program you will receive different address values because every time new memory will be allocated.

We can also use ‘%u’ format specifier in place of ‘%d’ while printing address values. ‘%u’ is used for the memory address to give unsigned values.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.