Linux C Programming Tutorial Part 19: Pointers and Arrays

Up until now in this ongoing C programming tutorial series, we have briefly discussed the basics of pointers. There are, however, several more pointers related concepts that need to be discussed. So in this tutorial, we will be discussing the concept of pointers and arrays.

Before we begin with pointers and arrays, let's quickly refresh the basic concept of pointers itself. Pointers are a special kind of variables that can hold addresses. For example, a pointer 'ptr' to a character can be declared in the following way;

char *ptr;

And you can use the ampersand sign to store an address into it. Here's an example:

char c = 'a';
ptr = &c;

So now, pointer 'ptr' contains the address of variable 'c'. Alternatively, you can also say that 'ptr' now points to variable 'c'.

To access or manipulate the value of the variable pointed to by the pointer, you can use the * operator. For example, to change the value of variable c from 'a' to 'b', you can use the pointer 'ptr' in the following way:

*ptr = 'b';

The following program should give you a better idea on these basic pointer concepts we've discussed so far:

#include <stdio.h>

int main()
{
char c = 'a';
char *ptr = &c;

*ptr = 'b';

printf ("variable c = %c", c);

return 0;
}

Here's the output of this program:

variable c = b

So you can see using a pointer, the value of a variable was changed.

Pointers and Arrays

As you will spend more and more time writing and reading C code, you'll observe that pointers and arrays are often used together. For example, you can access individual elements of an array using a pointer. Consider the following piece of code:

...
...
...
char arr[] = {'a','b','c','d','e'}
char *ptr;
ptr = &arr[0]
...
...
...

Here, pointer 'ptr' points to the first element of the array 'arr', which if you talk in terms of indexes, sits at position zero of the array. Now, you can use the * operator with 'ptr' to access/manipulate the value which the pointer is pointing to.

Moving on, once you have a pointer pointing to the first element of an array, it's easy to access other elements of the array as well. For example, if - in context of the code excerpt above - you use the following expression anywhere:

*(ptr+1)

then it lets you access the second element of the array, something which you usually do use the following way:

arr[1] 

So effectively, adding 1 to 'ptr' takes to the address of the next element in the array, and using * you can access the value stored at that address. Similarly, adding 2 takes you to the third element ... so on and so forth.

Here's a program that should give you a clearer idea on what we discussed just now:

#include <stdio.h>

int main()
{
char arr[] = {'a','b','c','d','e'};
char *ptr = &arr[0];

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

return 0;
}

And here's the output:

arr[0] is a 
arr[1] is b
arr[2] is c
arr[3] is d
arr[4] is e

So you can see, the expression '*(ptr+i)' - with i varying from 0 to the last index in the array - allowed us to access all elements of the array.

NOTE 1: The name of the array, when used in the code, gives you the base address of the array. This means the name of the array and the address of its first element is one and the same thing. So in the context of an array 'arr', '&arr[0]' and 'arr' is one and the same thing. This extends to other elements as well, meaning &arr[2] can also be written as (arr+2) and so on.

NOTE 2: Extending what we discussed in the note above, every array element representation can be broken down to pointer representation. For example: arr[2] is equivalent to '*(arr + 2)'.

NOTE 3: While you can assign an array address to a pointer, vice versa is not valid. Also, unlike pointers, you can't use ++ or -- operators with array name. Broadly speaking, you can't change the addresses at which array elements are located originally.

Conclusion

This tutorial focused on the concept of pointers and arrays, how they are related and how array elements can be accessed through pointers. Towards the end, we also discussed some important points when it comes to code that utilizes pointers to arrays.

Do try out these concepts locally on your machine to get a better idea of how these things work, and in case of any doubt or query, leave a comment below.

Share this page:

0 Comment(s)