ARRAYS and Pointers IN C_1
ARRAYS and Pointers IN C_1
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To create an array, define the data type (like int) and specify the name of the array followed
by square brackets [].
To insert values to it, use a comma-separated list inside curly braces, and make sure all values
are of the same data type:
int myNumbers[] = {25, 50, 75, 100};
We have now created a variable that holds an array of four integers.
Access the Elements of an Array
To access an array element, refer to its index number.
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in myNumbers:
Example
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
// Outputs 25
Change an Array Element
To change the value of a specific element, refer to the index number:
Example
myNumbers[0] = 33;
Example
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
Real-Life Example
To demonstrate a practical example of using arrays, let's create a program that calculates the
average of different ages:
Example
// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
// Loop through the elements of the ages array to find the lowest age
for (i = 0; i < length; i++) {
if (lowestAge > ages[i]) {
lowestAge = ages[i];
}
}
MULTIDIMENSIONAL ARRAYS
if you want to store data as a tabular form, like a table with rows and columns, you need to
get familiar with multidimensional arrays.
A multidimensional array is basically an array of arrays.
Arrays can have any number of dimensions. The most common; two-dimensional arrays
(2D).
Two-Dimensional Arrays
A 2D array is also known as a matrix (a table of rows and columns).
To create a 2D array of integers, take a look at the following example:
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
The first dimension represents the number of rows [2], while the second dimension represents
the number of columns [3]. The values are placed in row-order, and can be visualized like
this:
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
C POINTERS
Creating Pointers
You learned from the earlier, that we can get the memory address of a variable with the
reference operator &:
Example
int myAge = 43; // an int variable
✓ A pointer is a variable that stores the memory address of another variable as its
value.
✓ A pointer variable points to a data type (like int) of the same type, and is created
with the * operator.
✓ The address of the variable you are working with is assigned to the pointer:
Example
int myAge = 43; // An int variable
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the address
of myAge
// Output the value of myAge (43)
printf("%d\n", myAge);
// Output the memory address of myAge (0x7ffe5367e044)
printf("%p\n", &myAge);
// Output the memory address of myAge with the pointer (0x7ffe5367e044)
printf("%p\n", ptr);
Example explained
✓ Create a pointer variable with the name ptr, that points to an int variable (myAge). Note
that the type of the pointer has to match the type of the variable you're working with
(int in our example).
✓ Use the & operator to store the memory address of the myAge variable, and assign it to
the pointer.
✓ Now, ptr holds the value of myAge's memory address.
Dereference
In the example above, we used the pointer variable to get the memory address of a variable
(used together with the & reference operator).
You can also get the value of the variable the pointer points to, by using the * operator
(the dereference operator):
Example
int myAge = 43; // Variable declaration
int* ptr = &myAge; // Pointer declaration
// Reference: Output the memory address of myAge with the pointer (0x7ffe5367e044)
printf("%p\n", ptr);
// Dereference: Output the value of myAge with the pointer (43)
printf("%d\n", *ptr);
Note that the * sign can be confusing here, as it does two different things in our code:
✓ When used in declaration (int* ptr), it creates a pointer variable.
✓ When not used in declaration, it acts as a dereference operator.
Good To Know: There are two ways to declare pointer variables in C:
int* myNum;
int *myNum;
Notes on Pointers
Pointers are one of the things that make C stand out from other programming languages,
like Python and Java.
They are important in C, because they allow us to manipulate the data in the computer's
memory. This can reduce the code and improve the performance. If you are familiar with data
structures like lists, trees and graphs, you should know that pointers are especially useful for
implementing those. And sometimes you even have to use pointers, for example when
working with files and memory management.
But be careful; pointers must be handled with care, since it is possible to damage data stored
in other memory addresses.