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:
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:
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 :
We can also do the same without explicitly specifying the array size. Consider the example below:
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
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:
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:
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:
Declaring a Pointer
To declare a pointer in C, we use the type followed by an asterisk and the variable’s name.
For example:
To assign an address to the pointer, we can do:
ptr = &i;
In the example above, we set the address of the variable “i” to *ptr.
Accessing Values from a Pointer
We can access the values stored in a pointer address by using the asterisk (*) notation. For example:
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:
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:
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 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.