HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

C Pointers and Arrays



Pointers & Arrays

Pointers and arrays are fundamental concepts in C programming, and understanding their relationship is crucial for effective memory management and advanced data manipulation.

Consider the following array of integers:

Example

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

You learned from the arrays chapter: for loops provide a structured way to loop through the elements of an array. They offer efficient and readable code for iterating over each element and performing operations on them.

Example

#include <stdio.h>

int main() {

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

int i;

for (i = 0; i < 4; i++) {

printf("%d\n", myNumbers[i]);

}

return 0;

}

Output

25

50

75

100

o print the memory address of each array element in C, you can utilize the address-of operator (&). Here's the modified code:

Example

#include <stdio.h>

int main() {

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

int i;

for (i = 0; i < 4; i++) {

printf("%d\n", myNumbers[i]);

}

return 0;

}

Output

0x7ffe70f9d8f0

0x7ffe70f9d8f4

0x7ffe70f9d8f8

0x7ffe70f9d8fc

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

#include <stdio.h>

int main() {

int myInt;

printf("%lu", sizeof(myInt));

}

return 0;

}

Output

4

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

#include <stdio.h>

int main() {

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

printf("%lu", sizeof(myNumbers));

}

return 0;

}

Output

16


How Are Pointers Related to Arrays

  • pointers and arrays have a unique and intertwined relationship:
  • Array Name as a Pointer: In C, the name of an array itself decays to a constant pointer pointing to the first element of the array. This means you can use the array name interchangeably with a pointer to its first element in many contexts.
  • Array Indexing vs. Pointer Arithmetic: While the array name behaves like a pointer, accessing elements using array indexing (numbers[i]) and pointer arithmetic (ptr[i]) is slightly different:

  • The memory address of the first element is the same as the name of the array:

    Example

    #include <stdio.h>

    int main() {

    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]);

    }

    return 0;

    }

    Output

    0x7ffe70f9d8f0

    0x7ffe70f9d8f0


    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

    #include <stdio.h>

    int main() {

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

    // Get the value of the first element in myNumbers

    printf("%d", *myNumbers);

    return 0;

    }

    Output

    25


    To access the rest of the elements in myNumbers, you can increment the pointer/array (+1, +2, etc):

    Example

    #include <stdio.h>

    int main() {

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

    // Get the value of the second element in myNumbers

    vprintf("%d\n", *(myNumbers + 1));

    // Get the value of the third element in myNumbers

    printf("%d", *(myNumbers + 2));

    return 0;

    }

    Output

    50

    75


    Or loop through it:

    Example

    #include <stdio.h>

    int main() {

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

    int *ptr = myNumbers;

    int i;

    for (i = 0; i < 4; i++) {

    printf("%d\n", *(ptr + i));

    }

    return 0;

    }

    Output

    25

    50

    75

    100


    It is also possible to change the value of array elements with pointers:

    Example

    #include <stdio.h>

    int main() {

    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));

    return 0;

    }

    Output

    13

    17