🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #58

Pointers & Arrays


Pointers & Arrays

You can also use pointers to access arrays.

Consider the following array of integers:

Example

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

You learned from the arrays chapter that you can loop through the array elements 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]);
}

Instead of printing 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]);
}

Note that the last number of each of the elements' memory address is different, with an addition of 4.

It is because the size of an int type is typically 4 bytes, remember:

Example

    // Create an int variable
int myInt;

// Get the memory size of an int
    printf("%zu", sizeof(myInt));

So from the "memory address example" above, you can see that the compiler reserves 4 bytes of memory for each array element, which means that the entire array takes up 16 bytes (4 * 4) of memory storage:

Example

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

// Get the size of the myNumbers
    array
printf("%zu", sizeof(myNumbers));

Ok, so what's the relationship between pointers and arrays? Well, in C, the name of an array, is actually a pointer to the first element of the array.

Confused? Let's try to understand this better, and use our "memory address example" above again.

The memory address of the first element is the same as 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]);

This basically means that we can work with arrays through pointers!

How? Since myNumbers is a pointer to the first element in myNumbers, you can use 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);

To access the rest of the 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..

Or loop through it:

Example

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

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

It is also possible to change the value of array elements with 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));

Note: This way of working with arrays might seem a bit excessive. Especially with simple arrays like in the examples above. However, for large arrays, it can be much more efficient to access and manipulate arrays with pointers. It is also considered faster and easier to access two-dimensional arrays with pointers. And since strings are actually arrays, you can also use pointers to access strings. For now, it's great that you know how this works. But like we specified in the previous chapter; pointers must be handled with care, since it is possible to overwrite other data stored in memory.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Pointers & Arrays – FAQs

Quick answers about learning Pointers & Arrays in C.

This free note from CodingNow 2.0 explains Pointers & Arrays in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including Pointers & Arrays, is 100% free with no signup required.
With focused practice, most students grasp Pointers & Arrays in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now