POINTER & ARRAYS

Dev Frank
3 min readFeb 17, 2024

--

Understanding C Pointers and Arrays

In the realm of C programming :-) , pointers and arrays share a close connection, providing a versatile approach to data manipulation.

Pointers & Arrays:

Arrays in C, such as the integer array below, can be traversed using pointers:

int dataValues[4] = {30, 60, 90, 120};

Looping through array elements using a for loop:

int dataValues[4] = {30, 60, 90, 120};
int iterator;

for (iterator = 0; iterator < 4; iterator++) {
printf("%d\n", dataValues[iterator]);
}

Result:

30
60
90
120

Rather than printing array values, let’s print the memory address of each element:

int dataValues[4] = {30, 60, 90, 120};
int iterator;

for (iterator = 0; iterator < 4; iterator++) {
printf("%p\n", &dataValues[iterator]);
}

Result:

0x7ffe70f9d8f0
0x7ffe70f9d8f4
0x7ffe70f9d8f8
0x7ffe70f9d8fc

Notice that the last digits in each element’s memory address differ, incrementing by 4, reflecting the typical size of an integer (4 bytes).

Pointers and Arrays Relationship:

In C, the array’s name is essentially a pointer to its first element. Observe this with the memory address example:

int dataValues[4] = {30, 60, 90, 120};

// Get the memory address of the array
printf("%p\n", dataValues);

// Get the memory address of the first array element
printf("%p\n", &dataValues[0]);
0x7ffe70f9d8f0
0x7ffe70f9d8f0

This implies that arrays can be manipulated using pointers.

Working with Pointers and Arrays:

Utilizing the pointer concept, you can access array elements using the * operator:

int dataValues[4] = {30, 60, 90, 120};

// Get the value of the first element
printf("%d", *dataValues);

Result:

30

To access subsequent elements, increment the pointer/array (+1, +2, etc.):

int dataValues[4] = {30, 60, 90, 120};

// Get the value of the second element
printf("%d\n", *(dataValues + 1));

// Get the value of the third element
printf("%d", *(dataValues + 2));

Result:

60
90

Looping through the array using pointers:

int dataValues[4] = {30, 60, 90, 120};
int *ptr = dataValues;
int iterator;

for (iterator = 0; iterator < 4; iterator++) {
printf("%d\n", *(ptr + iterator));
}

Result:

30
60
90
120

Manipulating array values with pointers:

int dataValues[4] = {30, 60, 90, 120};

// Change the value of the first element to 13
*dataValues = 13;

// Change the value of the second element to 17
*(dataValues + 1) = 17;

// Get the value of the first element
printf("%d\n", *dataValues);

// Get the value of the second element
printf("%d\n", *(dataValues + 1));

Result:

13
17

Distinguishing Pointers from Arrays

In most cases, accessing pointers and arrays can be considered similar, with some notable exceptions:

  1. Size Information:
    sizeof(array) provides the total memory occupied by all elements in the array.
    sizeof(pointer) only indicates the memory consumed by the pointer variable itself.

2. Address Operators:
array is synonymous with &array[0] and yields the address of the first array element.
&pointer gives the address of the pointer variable itself.

3. String Literal Initialization:
char array[ ] = “abc”` initializes the array with ‘a’, ‘b’, ‘c’, and ‘\0’.
char *pointer = “abc”` sets the pointer to the address of the “abc” string (often stored in read-only memory).

4. Assignment of Values:
Pointer variables can be assigned values, e.g., int *p; p = a; is legal.
However, assigning an array to another array directly, like a = p; , is illegal.

5. Arithmetic Operations:
Arithmetic operations on pointer variables are allowed, e.g., p++; is legal.
Performing arithmetic on an array, such as a++; , is not permitted.

6. Data Type Nature:
An array is a collection of identical data types.
A pointer variable stores the memory address of a data type, providing flexibility in data handling.

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.