Learn pointers in C in a few easy steps

learn pointers in c in a few easy steps

learn pointers in c in a few easy stepsIf you are having problems understanding pointers then you have come to the correct place. I came across pointers for the first time when I was in class 9 and initially I found it a little difficult. Only when I had to do a project on pointers I understood the concepts properly. In the project I had used a lot of pictures to explain the concepts and recently I got hold of the project and I decided to share it with you. Though I had made a few changes to the original version I’m sure this post will help you in understanding the concepts of pointers.

#1: Now what the hell is a pointer?

A pointer is something that points to another thing(duh). Even the man pointing the gun is a pointer. Seriously pointers are variables that are used to point to other variables. i am a pointer

#2: But how do the pointers point to other variables?

Every variable is stored in the memory. Memory is organised as a sequence of byte sized(1 byte = 8 bits) locations. These bytes are numbered beginning with zero. The number associated with a byte is known as its address. Now the pointer stores this address and  point to other variables. So a pointer is a variable that contains a memory address.

#3: How do I find the address of a variable?

Simple use the Global Positioning System(GPS)  :mrgreen:. Seriously use the address operator(&) to find out the address of a variable.

address of a variable using & operator

#4: Enough with the concepts I just want to write a program

Patience dear. First thing you have to¬†know¬†is how a pointer variable looks like. Well it looks just like any other variable except for the ‘*‘ part before its name to signify that it’s a pointer. And a pointer variable has a type just like other variables.


int *p; //a pointer of type int and name p.

Write programs on pointers
Let’s start with a simple program to define a integer type variable and a pointer variable to store it’s address. Then print the value of the pointer.


#include<stdio.h>
#include<conio.h>
int main(){
 int x; //the variable
 int *p; //the pointer
 p = &x; //stores the address of the variable in the pointer
 printf("The address of x is %p", p); //prints the value of the pointer.
 //%p is used for pointers. %x can also be used
 getch();
 return 0;
}

#5: What will I even do with the address of a variable?

You can have entire control of the variable from the address. You can not just define it’s value and modify it. Also you can do nasty things with it. Don’t have to believe me I will show you how. Here comes the famous indirection(*) operator I was talking about. Indirection operator is used as a prefix to the pointer variable to directly affect the value of the variable it points to also known as dereferencing. The term dereferencing means accessing the data in a memory location pointer to by a pointer. Getting confused? Look below.

indirection operator pointers

#6: Show me how I can do the nasty things¬† ūüėČ

nasty things using pointers

Oh now you are interested. Let’s write a program that can modify the value of a variable using a pointer.


#include<stdio.h>
#include<conio.h>
int main(){
 int x = 10;
 int *p;
 p=&x;
 printf("Value of x = %d\n",x); //initial value of x is 10
 *p = 20; //modifying the value of x using pointers
 printf("Value of x is now %d",x); //now points for guessing the value of x now
 getch();
 return 0;
}

#7: Is it really important for the pointers to have a type? After all it’s just an address

The answer to this question is YES. But if you ask if it’s necessary then it’s NO. There’s something called void pointer but I’ll come to that later. Let me tell you why it is important. We should define what kind of values the pointer is going to point to so that it can be dereferenced using indirection(*) operator. Pointers defined to be of a specific type cannot hold the address of any other type of variable. The exception is void pointer which does not have any type. So it can point to any variable but it cannot be dereferenced like any other pointer variables. To dereference a pointer to void, it must be suitably typecast to the required type. Let’s see how you can access the value using void pointers.

</pre>
#include<stdio.h>
#include<conio.h>
int main(){
 int x = 10;
 char y = 'a';
 float z = 30.20;
 int *p1;
 void *p2;
 //the following line is not allowed
 //p1 = &y;
 p2 = &x; //void pointer p2 points to an int
 //type casting to an int pointer
 printf("Value of *p2 is %d\n", *(int *)p2);
 p2 = &y; //p2 points to a char
 //type casting to an char pointer
 printf("Value of *p2 is %c\n", *(char *)p2);
 p2 = &z; //p2 points to a float
 //type casting to an float pointer
 printf("Value of *p2 is %f\n", *(float *)p2);
 getch();
 return 0;
}
<pre>

#8: If I am not wrong I have seen something like int **p. What does that mean?

Pointers are also variables so they are also stored in memory and therefore have an address. It’s possible to store the address of an pointer in another pointer variable which is known as an pointer to a pointer(**p). Similarly there can be a pointer to a pointer to a pointer(***p).

pointer to a pointer to a pointer

Let’s write a program where we use a pointer, a pointer to a pointer and a pointer to a pointer to a pointer.


#include<stdio.h>
#include<conio.h>
int main(){
 int n = 10;
 int *ptr; //pointer
 int **ptrToPtr; //pointer to a pointer
 int ***ptrToPtrToPtr; //pointer to a pointer to a pointer
 ptr = &n; //ptr points to n
 //ptrToPtr points to ptr
 ptrToPtr = &ptr;
 //ptrToPtrToPtr points to ptrToPtr
 ptrToPtrToPtr = &ptrToPtr;
 printf("Value of n = %d\n",n);
 *ptr = 20;
 printf("Value of *ptr = %d\n",*ptr);
 **ptrToPtr = 30;
 printf("Value of **ptrToPtr = %d\n",**ptrToPtr);
 ***ptrToPtrToPtr = 40;
 printf("Value of ***ptrToPtrToPtr = %d\n",***ptrToPtrToPtr);
 getch();
 return 0;
}

#9: I heard there’s something called far and huge pointers. What are they?

You must have heard about them from your grandfather because they are deprecated in Win32. At your grandfather’s time when they used 16 bit compilers these were prevalent. Far and huge pointers has both the segment(16 bit) and the offset address(16 bit), total size=32 bits. The difference between far and huge pointer is the¬†compiler rounds off the offset of a far pointer to zero when the offset reaches 0xFFFF but for a huge pointer, it increments the segment value on reaching 0xFFFF.¬†Thus huge pointers can be increased or decreased uniformly between any segments and can have any value from 0 to 1 MB. The near pointer on the other hand holds only the offset address so it’s size is 16 bits.

far and huge pointers

#10: Can a pointer also point to a function?

As a matter of fact a pointer can point to a function.¬†The type of a pointer to a function is based on both the return type and parameter types of the function.¬†A declaration of a pointer to a function must have the pointer name in parentheses. Let’s see them in action.


#include<stdio.h>
#include<conio.h>

int square(int n){
 return n*n;
}

int main(){
 int (*p)(int); //pointer to a function
 p = square; //assigning the value to the pointer
 printf("Answer = %d",p(10)); //calling the function using pointer
 getch();
 return 0;
}

#11: What happens when I add or subtract from a pointer?

Addition and subtraction operations on a pointer is very interesting. Unary operators such as ++ and — and binary operators such as + and – can be used with pointers. When we add 1 to a pointer of data type ‘x’ or write ptr++, the value becomes current address in pointer + 1 * sizeof(‘x’). So when the type is int, the address will get incremented by 2 and not by 1. Similarly when subtraction is done the address will decrement accordingly.

pointer addition

Finally this long tutorial has come to an end. You can have plenty of fun with pointers. Hopefully you enjoyed reading this tutorial. If you have any doubts please use the comments.  Thank you and visit www.intechgrity.com for more tutorials.

2 comments

  1. Jayanthi Reply

    its awesome.ur explanation really help me a lot.keep ur doing.so that I can enhance our knowledge in c and java.Wonderful explanation.omg…….

Leave a Reply