C Programming

Create and Use Array of Pointers in C

Arrays and pointers are among the most fundamental data structures in the C language. They allow us to create flexible and easy-to-manage programs with only a few lines of code.

Most of us are familiar with creating arrays with data types such as integers, characters, or floats. This guide will show you how to create an array of pointers and use it to store data.

How to Create an Array

We can define an array as a collection of items. For example, an array can store names of 100 people and such.

To declare an array in C, we use the syntax:

dataType arrName[arrSize];

Here, the dataType refers to the type of array, which can be an integer, float, a character, or a pointer.

The arrName refers to the name given to the array, which can be any descriptive term for the variable as long it obeys the rules of naming a variable in C.

Finally, the arrSize refers to the total number of items to store in the array. This value is constant and unchangeable once declared.

For example, we can define an array of integers to store 10 values as:

int myarray[10];

We can also define and initialize an array in the same line. For example, to define the array above and populate it with the required values, we can do :

int myarray[10] = {1,2,3,4,5,6,7,8,9,10};

We can also do the same without explicitly specifying the array size. Consider the example below:

int myarray[] = {1,2,3,4,56,7,8,9,10};

To access items in an array, we use the indexing method by specifying the array’s name followed by the index of the item we wish to access.

For example, to access the item at index 5, we can do

printf(%d”, myarray[5]);

In this case, it should return the 6th item in the array because array indexing starts at 0. Therefore, the first item is at 0, the second item at 1, and so on.

We can also use the above method to modify the values in an array.

Here is an example:

myarray[5] = 5;

The above statement will change the array value at index 5 to 5.

C Pointers

Pointers are variables that we can use to store addresses of values in a program. For example, when you define a variable, it is assigned a memory address that you can access by using the
&varname;

For example:

int i = 10;
printf(%p”, &i);

The above code should give the address for the “i” variable. Note that this address can vary every time you run the program.

Here is a sample address:

0061FF1C

Declaring a Pointer

To declare a pointer in C, we use the type followed by an asterisk and the variable’s name.

For example:

int *ptr, i;

To assign an address to the pointer, we can do:

int i = 10;
ptr = &i;

In the example above, we set the address of the variable “i” to *ptr.

int *ptr, i;
i = 10;
ptr = &i;
printf("%p", *ptr);

Accessing Values from a Pointer

We can access the values stored in a pointer address by using the asterisk (*) notation. For example:

int *ptr, i;
i = 10;
ptr = &i;
printf("%d", *ptr);

In this case, we get the specific value stored and not the address of the value.

Array of Pointers

As we created an array of integer values in the examples above, we can create an array of a pointer—basically, an array that stores memory addresses.

To do this, we can use the syntax:

int *ptrarray[10];

In this example, we have an array of 10 integer pointers, allowing you to store memory addresses of 5 integer variables.

For example, we can have the simple code below:

#include <stdio.h>

int main() {
    int *ptrarray[4];
    int w = 100, x = 200, y = 300, z = 400;

    ptrarray[0] = &w;
    ptrarray[1] = &x;
    ptrarray[2] = &y;
    ptrarray[3] = &z;

    for (int i = 0; i< 4; i++) {
         printf("The value %d has the adddress %d\n", *ptrarray[i], ptrarray[i]);
    }
    return 0;
}

Once we compile and run the code above, we should get results similar to the ones below:

The value 100 has the adddress 6422280
The value 200 has the adddress 6422276
The value 300 has the adddress 6422272
The value 400 has the adddress 6422268

Using this method, we can access both the addresses and the values stored in the array pointer.

Conclusion

In this short guide, we discussed how to use arrays and pointers in the C language. We also discussed how to create an array of pointers to store addresses for various values.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list