Pointers in C Programming

Today I will not introduce you to any advance feature of function. Before proceeding further to our next tutorial about call by value and call by reference, it is compulsory to learn the basic concept of pointers. It is used in the advance feature of function that is call by reference. Pointer is one of the most difficult concept in C programming. So I recommend you to read this tutorial with concentration. In this tutorial I will only give you a basic overview of pointers.

What are Pointers in C?

In the first tutorial I told you how values are stored in C. I told you that every variable has an address. So a pointer is variable which stores address of another variable. Remember here address term is quite important. Pointers can only store addresses of other variable.

Pointers in C Programming
Pointers in C – Image Source

int x=10, y=20;
printf(“%u %u”, &x, &y);

Here %u is a format specifier. It stands for unsigned, so it will only display positive values.

You will get output of the above program like below.
605893 605897

Well these are the addresses of the variable x and y. Here & is the “address of” operator. It is used to take the compiler to the address of variables. Address of any variable can’t be negative. This is the reason we have used %u format specifier to print the address of variables on the screen.

value at address (*) Operator

This is the second operator used for pointers. It is used to access the value present at some address. And it is used to declare a pointer.

Note: Pointer values and integer values are entirely different. We cannot store any address in integer variable. To store address of any variable we have to declare one variable as pointer.

Declaration and initialization of pointers

int x=10;
int *y; // Declaration of Pointer variable
y=&x; // Storing address of x variable in y pointer variable

Usage of pointers

int a=3;
int *b;
b=&a;
printf(“Value at address %u is %d”, b, *b);

The output of above code will be something like given below.
Value at address 605764 is 3

Explanation

  • In the first statement I have declared the integer variable i and stored the value 3 in it.
  • In the second statement I have declared b variable as pointer by using (*) operator. Remember that it can store only the address of integer type variable.
  • After that I have stored address of variable a in pointer variable b.
  • Now in printf() function I have printed the address inside b pointer and value at address inside pointer variable b.

Note: In our program pointer variable b contains the address of integer variable a. To store address of float, char or any other type variable, we have to declare a pointer of that type.

To better understand the concept of pointers I would recommend you to run the below program at home and try to figure out the result by your own.

Predict the output of above program first and run it to check the results of it. Comment below if you find any difficulty in understanding this program.

Output

Pointers in C Programming - Output

As I said earlier concept of pointer is compulsory before learning the topic call by reference. I hope till now you must have a good grip on basic concepts of pointer. So in the next tutorial I will give you a tutorial on call by value and call by reference feature of functions.

2 thoughts on “Pointers in C Programming”

Leave a Comment

Your email address will not be published. Required fields are marked *