Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Pointers and Arrays

Pointers & Arrays

You can also utilize pointers for accessing arrays.

Take into account the subsequent array of integers:

Example

int myNumbers[4] = {25, 50, 75, 100};

From the arrays chapter, you gained knowledge that looping through the array elements can be achieved with a for loop.

Example

int myNumbers[4] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf(“%d\n”, myNumbers[i]);
}

Result:

25
50
75
100

Rather than outputting the value of each array element, let’s print the memory address of each array element.

Example

int myNumbers[4] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {
  printf(“%p\n”, &myNumbers[i]);
}

Result:

0x7ffe70f9d8f0
0x7ffe70f9d8f4
0x7ffe70f9d8f8
0x7ffe70f9d8fc

Notice that the last digit of each element’s memory address differs, increasing by 4.

This discrepancy arises due to the typical size of an int type being 4 bytes; recall:

Example

// Create an int variable
int myInt;

// Get the memory size of an int
printf(“%lu”, sizeof(myInt));

Result:

4

As evidenced by the “memory address example” provided above, the compiler allocates 4 bytes of memory for each array element. Consequently, the entire array occupies 16 bytes (4 * 4) of memory storage.

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the size of the myNumbers array
printf(“%lu”, sizeof(myNumbers));

Result:

16

How Are Pointers Related to Arrays

In C, the relationship between pointers and arrays is notable. Specifically, the name of an array effectively serves as a pointer to the first element of that array.

Feeling perplexed? Let’s delve deeper into this concept using our aforementioned “memory address example” once more.

The memory address of the initial element coincides with the name of the array:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the memory address of the myNumbers array
printf(“%p\n”, myNumbers);

// Get the memory address of the first array element
printf(“%p\n”, &myNumbers[0]);

Result:

0x7ffe70f9d8f0
0x7ffe70f9d8f0

Essentially, this implies that we can manipulate arrays using pointers!

How? Given that “myNumbers” serves as a pointer to the first element in the array “myNumbers”, you can utilize the * operator to access it:

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the value of the first element in myNumbers
printf(“%d”, *myNumbers);

Result:

25

To access the remaining elements in “myNumbers”, you can increment the pointer/array (+1, +2, etc.):

Example

int myNumbers[4] = {25, 50, 75, 100};

// Get the value of the second element in myNumbers
printf(“%d\n”, *(myNumbers + 1));

// Get the value of the third element in myNumbers
printf(“%d”, *(myNumbers + 2));

// and so on..

Result:

50
75

Alternatively, you can traverse it with a loop.

Example

int myNumbers[4] = {25, 50, 75, 100};
int *ptr = myNumbers;
int i;

for (i = 0; i < 4; i++) {
  printf(“%d\n”, *(ptr + i));
}

Result:

25
50
75
100

It’s also viable to modify the values of array elements using pointers.

Example

int myNumbers[4] = {25, 50, 75, 100};

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

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

// Get the value of the first element
printf(“%d\n”, *myNumbers);

// Get the value of the second element
printf(“%d\n”, *(myNumbers + 1));

Result:

13
17

 

This approach to handling arrays might appear somewhat intricate, especially with straightforward arrays like those in the examples provided above. However, for larger arrays, leveraging pointers can significantly enhance efficiency in accessing and manipulating array elements.

Accessing two-dimensional arrays with pointers is generally regarded as faster and more straightforward.

Moreover, given that strings are essentially arrays, pointers can also be employed to access strings.

It’s beneficial to understand how this mechanism functions at the moment. However, as emphasized in the preceding chapter, caution must be exercised when dealing with pointers, as unintended overwriting of other data stored in memory is a possibility.