Linux C Programming tutorial Part 21: Character pointers, array of pointers, and pointer to pointer

The concept of pointers is indeed one of the very important concepts in the C programming language. Up until now, we have discussed several aspects of pointers in C. Expanding on that, in this tutorial, we will be discussing a few more pointer concepts.

Character pointers, array of pointers, and pointer to pointer in C

Let's begin with character pointers with the following lines of code:

char p[] = "I like HowtoForge"
char *p = "I like HowToForge"

The first line defines an array 'p' with size equal to the number of characters in double quotes. But the next line defines a pointer 'p' which points towards a string constant.

The difference here is that the first 'p' being an array, you can easily modify or change the contents of the array. But since the second 'p' is pointing to a string constant, you just can't change the string contents. 

For example, the following piece of code tries to modify a string constant:

#include <stdio.h>

int main()
{
char *p = "I like HowToForge";

p[0] = 'U';

return 0;
}

And here's the output produced by this code on my system:

Segmentation fault

This error suggests the program execution came to an abrupt end, and that's because we tried to change something which is constant.

Also, keep in mind that while the pointer 'p' can be made to point a different string, you cannot change the base address of the array 'p' (if you remember, this we have already discussed in one of our previous tutorials).

Now moving on to pointer arrays, just like you've seen integer, character, and another type of arrays, there can also be an array of pointers. For example, the following program defines an array 'arr' of integer pointers and assigns values to it.

#include <stdio.h>

int main()
{
int *arr[3];
int a = 0, b = 1, c = 2;

arr[0] = &a;
arr[1] = &b;
arr[2] = &c;

return 0;
}

Note that the values assigned to the array are addresses. This is because 'arr' is an array of pointers, and pointers store nothing but addresses. Now, if you want to access values kept at these addresses, you'll have to use the *operator.

Following example (which is nothing but an extension of the previous example) showcases this:

#include <stdio.h>

int main()
{
int *arr[3];
int a = 0, b = 1, c = 2;

arr[0] = &a;
arr[1] = &b;
arr[2] = &c;

for(int i=0; i < 3; i++)
printf("\n arr[%d] is: %d",i,*(arr[i]));

return 0;
}

Here's the output:

arr[0] is: 0 
arr[1] is: 1
arr[2] is: 2

Similar to integer pointer arrays (like one we discussed here), you can have arrays storing character pointers and more. 

Now, let's move on to pointer to pointers. As we have iterated a number of times so far, a pointer stores an address. Now, up till now in this ongoing C programming tutorial series, we have only seen a pointer pointing to a non-pointer variable, but the fact is pointers can point to other pointers as well.

This means a pointer can store an address of another pointer. For example, following is a double pointer or a pointer to pointer:

int **ptr;

Here's is a piece of code that utilizes a double pointer:

#include <stdio.h>

int main()
{
int *ptr;
int **p;

int a = 10;

ptr = &a;

p = &ptr;

printf("\n Pointer 'p' points to pointer 'ptr' which further points to value: %d", **p);

return 0;
}

Here's the output:

Pointer 'p' points to pointer 'ptr' which further points to value: 10

So this was an example of a double pointer. On similar lines, you can have a pointer to a pointer to a pointer, defined as, for example, int ***ptr. The maximum number of such 'pointer to pointer to......' levels is implementation specific (in some cases the limit is 12 though).

Practically, however, you'll likely only encounter pointer to pointers up to level three, as having more levels makes the logic more complex to understand and maintain.

Conclusion

We discussed three important pointers related concepts here. You are advised to try out the examples and concepts we discussed here on your system to get a better idea of how these things work. In case of any doubt or query, drop us a comment below.

Share this page:

0 Comment(s)