1st semester engineers’ nightmare, POINTERS in C

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.

*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:

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.

Archives

We are a couple of enthusiastic web designers inspired by the latest technologies in web-development and the power of a well optimized website in boosting the client's business. Our solutions are always the best one has to offer, packing both affordability, extensive power, and Scalability. Our end-products are guaranteed to uphold to the clients wants, needs, and expectations. Our mission is to customize the web site in accordance to the need of the client making it user friendly, keeping in mind the aesthetic & technical aspects to be the best.