pointers in c

1st semester engineers’ nightmare, POINTERS in C

Posted on Posted in Technology

I still remember those 1st semester ICP classes in my alma mater, Assam Engineering College, when most of the professor’s lecture went above our heads, but the most frustrating topic of all was POINTERS in C. First of all it was a very complex topic to understand and secondly we didn’t even know what was the point of using it. Variables as themselves were enough as far as we were concerned. After all those complex programs with loops, arrays and functions, pointers was the fatal blow!!!

I remember our C++ instructor during our training at Accenture asking us to rate ourselves in the knowledge of C language. After our bizarre ratings, he told that he rated himself 1 out of 10. He said even Dennis Ritchie (who invented C) rated himself 7 out of 10. He then told us about the story of a girl he interviewed for a job vacancy, who rated herself 9 out of 10. On being asked why she didn’t rate herself 10, she said it was because she didn’t understand pointers. Here lies the irony, our instructor rated himself 1 just because he had a good understanding of pointers. According to him pointers are just the beginning of C, not the end of it.

So through this blog post I would like to help my younger brothers and sisters in their first encounter with C, to cope up with this nightmare called pointers. I’ll try to explain the general meaning of pointers and the reason why it is required in general programming and particularly in C.

 

 

Pointers in C:

Pointers are what the word literally means. They point to something. That something can be the location in the memory to a variable, an array or a structure. In object oriented programming languages like C++, they can point to objects. For a day to day example, a pointer can point to any file in your computer. Have you noticed that when you cut-paste a (big)file from one drive to another, it takes some time. But when you do the same within a drive, it happens instantaneously. It is because when you cut-paste to a different drive, the contents(actual file) has to be moved, but when it is done within the drive, the pointer to that file is simply redirected to the new location. Similarly deleting a file simply means deleting the pointer to the memory location of that file. So to completely erase data from a hard drive tech guys reset all bits to zero.

Coming back to C, let me explain the necessity of pointers through a simple program. Consider swapping two numbers from their variables. This can be done easily within the main() function. But suppose we need to do that to a lot of variables/multiple times in our program. So we write a function where we pass the two variables as arguments. The function performs the operation and program execution comes back to the main function. So in the next line we print the variables and expect the values to be swapped.

Without using pointers:

#include<stdio.h>

void swap(int,int); //Declare the function

int main()
{
int a=20, b=10;
printf(“Before swapping: %d %d\n”, a, b);
swap(a,b);
printf(“After swapping: %d %d\n”, a, b);
return 0;
}

void swap(int p,int q)
{
int temp; //Create temporary integer

temp = p;

/*p got the value of a and q got the value of b when a and
b were passed as arguments */

p = q;  // i.e. p = 20 and q = 10

q = temp;

/* values of p and q are interchanged, so p=10, q=20,
but the original values of a and b remains unaffected */

}

Here although we pass the variables as arguments to the function, the temporary variables within the function are interchanged, not the original variables. Here is the output:

1

 

Now using pointers, we define pointers to those variables and pass the address of the variables as the arguments into the function, here is the code:

#include<stdio.h>

void swap(int*,int*); //Declare the function

int main()
{
int a=20, b=10;
int *x ,*y; //Create two pointers

x = &a; //Assign first pointer to address of(&) a

y = &b; //Assign first pointer to address of(&) b

printf(“Before swapping: %d %d\n”, a, b);
swap(x,y);
/* passing x and y as arguments is
same as passing &a and &b i.e. address of a and b */

printf(“After swapping: %d %d\n”, a, b);
return 0;
}

void swap(int *p,int *q)
{
int temp; //Create temporary integer

temp = *p; //*p represents the value stored in memory location pointed by p

*p = *q;  // So we are interchanging the values in the two memory locations

*q = temp;
/*pointed by p and q, i.e. by x and y,
which are actually the variables a and b */

}

Inside the function the values in the locations specified by the arguments are interchanged. Thus the values at locations specified by p and q are swapped, i.e. values at locations specified by x and y are swapped. So effectively ‘a’ and ‘b’ are swapped.

Here is the output:

2

 

The first method of calling a function is called call by value where the values of variables are passed. The changed made to these values inside the function remains valid only within the scope of the function, the changes are not reflected outside the function.

The second method of calling a function is called call by reference where the addresses of variables are passed. The changed made to these values inside the function affects the actual values of the variables in the whole program.

Pointers are used to point to strings, arrays, structures and objects. They are very important in creating custom data structures like linked lists and queues. We’ll discuss those topics in future blogs.

Comment below if you want to further discuss this topic. Like our facebook page.

See you next time.

Comments

comments