Introduction to Pointers in C

lionaneesh's Avatar author of Introduction to Pointers in C
This is an article on Introduction to Pointers in C in C.
Rated 5.00 By 1 users
I am doing C programming from a couple of years ago and finally I convinced myself and grabbed all my guts to write an article on Pointers. Now, one could easily question Why? Basically, Pointers are the most useful part of C and usually the most difficult to understand.

Note: I don't want to discourage my readers, that learning about pointers can be a difficult but that's what most of the programmers say and according to me its the most exciting part of C Programming.

For the scope of this tutorial we'll only be looking at how to declare a pointer? , and the most basic pointer operators.

What is a Pointer



A pointer is a variable that contains the address in memory of another variable. We can have pointers for any data type in C, for example a long pointer , int pointer, char pointer etc. We also have a void pointer which does not have a type, thus it can be used to point to any data-type or even any kind of data, for the scope this tutorial we'll be not looking into any operations on void pointers. Void pointers are a special type of pointers in C and they need to be used in a different way which is out of the scope of this tutorial.

Operators



The unary operator '&' gives the adress of a variable.

Demonstration :-

Code:
#include<stdio.h>

int main()

{

int x = 0;

printf("Address of x: %p\n", &x);

}
Output :-

Code:
Address of x: 0xbf912a5c
Which would obviously give out a different address on different executions (Due to ASLR).

The dereference operator i.e * gives the contents of the object pointed by a pointer.

Declaring a pointer :-

To declare a pointer we follow the following format :-

Code:
[TYPE] *[NAME]
of course that's not a strict format and there are a number of ways to declare a pointer but for the scope of this tutorial, we'll be sticking to this one.

example :-

Code:
int *ptr
The above code declares an int pointer named “ptr”.

Note: Same as any C variable pointer also points to a garbage value (i.e address in case of pointers), and should always be initialized before using. Using uninitialized pointers can cause reading memory from an arbitrary address which will eventually result in a Segmentation fault i.e Invalid Reads or Writes depending on the usage.

Working with Pointers



example.c
Code:
#include<stdio.h>

int main()

{

int *ptr;

int x = 0;

ptr = &x;

printf("x : %d\n", *ptr);

}
Note: We must associate the pointer to its same type, i.e A long pointer should be associated with a long variable, and not with any other data type.

Consider a set of strict boxes, in which only a specific type of item can be contained, for instance a square box cannot hold a rectangular object. Similar is the case with the pointers. (Again I am not taking Void pointers into consideration as of this tutorial.)

Before reading the explanation below or before compiling, try to figure out what should be the output of the above program.

Output :-
Code:
x : 0
Explanation :-


5th line: Here we declare an int pointer named ptr.
6th line: Here we declare an int variable x and initialise it to 0.
7th line: Here we use the unary operator '&' to get the address of x and store it in ptr.

Now the pointer ptr contains the address of x, now the value of x can be accessed using the dereferencing operator.

9th line: Here we use the derefrencing operator which gives the value of the variable pointed by our pointer i.e x.

Now that might be a little difficult to grasp at first but believe me its easier than it looks.

Exercises :-

In this tutorial we were inroduced to basic usage of pointers in C, now I'd like the viewers to write some basic programs to test their knowledge.
  • Write a program to print the value of a long variable using pointers.
  • Write a program to print the value of a char variable using pointers.
Scripting's Avatar, Join Date: Jun 2010
John Hoder
Good tut man! Keep it up!
lionaneesh's Avatar, Join Date: Mar 2010
Invasive contributor
Quote:
Originally Posted by Scripting View Post
Good tut man! Keep it up!
Thanks
alaziaonline's Avatar
Banned
you shared a very informative information here thanks a lot for sharing
Davisandrian's Avatar, Join Date: Mar 2012
Newbie Member
Read your tut, its really helpful. Thanks for spreading.
samantha1230's Avatar
Banned
Me too get every time confused with pointers. Thats good at-least after long time you successfully understand about pointers. Theoretically they can be understood easily but at time of practical I became dumb.
honeylamb23's Avatar
Newbie Member
C programming is the hardest programming language I've tried..
tinytomhanks's Avatar
Banned
I am also start using c, i find it very resourceful. your telling style is very good.
sandeep552's Avatar
Banned
Nice one, thanks for introduce pointer.
tinytomhanks's Avatar
Banned
I feel happy to be here again, well as I know pointer is nothing but a variable which contain the address of others variables.