C series for 1st sem students: Pointers absolute basics

Posted on Posted in Blogs, Technology






Over the last couple of months we had covered the very basic concepts of programming using the C language. Now its time to tackle some advanced topics/concepts of programming. Over the next couple of months we’ll cover topics like pointers, memory management, data structures and file system. We will go slow, and include as many examples as possible. Today we are going to discuss about the very basics of pointers in C.

Pointer is something that points to the memory location where the value of a variable is stored. Pointers have data-types. This data-type has nothing to do with the value of the pointer itself, because a pointer has a value which is an address, i.e. a number. So the value of a pointer is always an int. But the data-type of the pointer denotes the data-type of the value stored in the memory location that it points to. For example, if a pointer points to the address where a character is stored, its data-type will be char whereas if that address has an integer stored in it, the data-type of the pointer will be int.

You have been already using pointers in your programs even if you haven’t realized. Consider the following commands.

You want to get the age of the user. So you create an age variable
int age;
To get the age from user you request the age via a printf command.

printf("Enter your age);
To store the user entered value, you use a scanf command
scanf("%d", &age);
Here &age is actually a pointer, which points to the actual address allocated in RAM, where the value of the variable age should reside. & is called the “address of” operator. To avoid using & operator everywhere, there is another way to initialize a pointer.

Lets see this example:

In line 5, we initialize a pointer b by prepending with an asterisk (*):
int *b = &a;

Here &a denotes the address of variable a, so we have created a pointer b which points to the RAM location of the variable a and the data-type of b is int because that RAM location holds an integer value. Now if we print the value of b we will get a number representing the RAM address. To get the value stored inside that address we write *b. Moreover, the variable b is also stored in some RAM location, so &b also gives us a pointer, which points to the address of b.

This is illustrated by the print statements in the program. I have marked the values printed by &a (address of a), and b (value stored inside b). We can see that both have the same value, i.e. the numerical address of the RAM location where value of a is stored during the execution of the program.

The actual value of the variable a is 10, which is also accessed by *b.

Thus we can see that the name of a pointer variable gives the address it points to. An asterisk(*) before the pointer name gives the value stored in that address. An ampersand(&) before the pointer name gives the address of that same pointer.

Char pointer: Now let us look at an example of a pointer of char data-type, i.e. it points to the address of a char variable.

Here we can see that ptr returns an integer (address of letter) and *ptr returns a character (value of letter).

Let us consider the use of data-type in assigning a pointer. You may wonder what’s the use of having a data-type of something that always holds an integer value (address). As we have seen in both examples, the value of the pointer is of the same data-type regardless of what type of variable it points to.

Well, the data-type of the pointer becomes significant when we do operations on them, like traversing through an array. For example, if a pointer points to an integer array (first item of the array), to get subsequent values we have to skip 4 bytes (int is 4 bytes long). But if a pointer points to a character array, to get the subsequent values we have to just go to the next byte (char is 1 byte long).

Assigning data-type to the pointer helps us accomplish that. Let us see the following example, where we increment pointers of different data-types.

Here b is an int pointer and ptr is a char pointer. On incrementing b we see that the address value increase by 4, while on incrementing ptr we see that the address value increase by 1.

These examples should help you understand the basic syntax and working principle of pointers. But we have not yet seen the different applications of pointers in C programs. On the next blog we will discuss the concepts of “pass by value” and “pass by reference” while passing arguments in a function, and the role of pointers in it.

You will find that the concept of pointers is not even present in modern languages. That doesn’t mean they don’t implement these concepts. They are actually handled in the language itself and the programmer is not exposed to them. Two of the main reasons behind this are:

  1. C was developed about 50 years ago, when computers had memory in the order of kilobytes. Newer languages all run on devices with gigabytes of memory.
  2. C was developed to write an operating system (Unix), so it needed to be more close to the hardware itself, like assembly languages. Modern languages run on top of operating systems, so they don’t need access to hardware.

You can download the code { here }





If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.

Comments

comments