I just can't understand the array and how it works in C?

It’s been quite a few days now and I just can’t understand what the array is all about in C.
I watched many videos and even read some articles but all that I was able to understand that array is bits grouped in a parameter that can store multiple values of it’s type, but I still didn’t understand much in how it works in C. Can someone please explain how it works in C and what the array is all about? Please try to explain it as simply as possible.

P.S - I don’t really understand what index 1 or index 0 is about.

Hey @himdas2011!

Not sure if you ever got it but an array is a collection of data stored in contiguous (meaning side by side) places in memory. So lets say we had int arr[] = {2, 5, 10, 7, 21}; So your system will look for spaces in memory that has 5 contingous blocks. When I ran the program:

#include <stdio.h>

int main()
{
    int arr[] = {2, 5, 10, 7, 21};
    int i;
    
    int length = sizeof(arr) / sizeof(arr[0]); // this is formula for length of arr.
    for (i = 0; i < length; i++) {
        printf("Address: %lu\n", &arr[i]); // I converted address to long unsigned int format so you can understand it better
        // but addresses are stored in hexadecimal. if you do %p instead of %lu, you will see.
    }
    return 0;
}

I got this (and a warning because I should be using %p, so don’t worry if you get a warning, you may not):

Address: 140732430094868
Address: 140732430094872
Address: 140732430094876
Address: 140732430094880
Address: 140732430094884

Notice how they are all 4 bytes apart (if we just look at the last two digits, 68 + 4 = 72, 72 + 4 = 76, 76 + 4 = 80, 80 + 4 = 84). I would also like to note that it won’t be the same on yours, if you run the program you will get different numbers each time. Here’s a small part (just focused on the array) of something similar to how it is in your system:

     Address    Offset      Int

140732430094868  - 0   \
140732430094869         \___ 2
140732430094870         /
140732430094871        /
140732430094872  - 1   \
140732430094873         \___ 5
140732430094874         /
140732430094875        /
140732430094876  - 2   \
140732430094877         \___ 10
140732430094878         /
140732430094879        /
140732430094880  - 3   \
140732430094881         \___ 7
140732430094882         /
140732430094883        /
140732430094884  - 4   \
140732430094885         \___ 21
140732430094886         /
140732430094887        /

In C, the datatype of the array must match the data elements. So since ints are usually 4 bytes, each of these ints are 4 bytes. Now with c arrays, an array just points to the first element in data. To get to the next element, it adds however many bytes the datatype is (so 4 in this case) by the position (also known as the offset). Indexes just tell the position of the element, arrays start from 0 (in most languages, there are some exceptions like LUA and R, and old programming langauges (in the 1950s) such COBOL and FORTRAN). Let’s say you have a book. Think of indexes like page numbers. The page number tells what number the page is relative to the others. If it has 1, it is the first page, 2, second page, 3, third page, 4, fourth page and so on.

The array points to the address of the beginning of the array. Which in this case is the address of arr[0]. To get the address of arr[0], you can write &arr[0]. Then to go to the second element it adds however many bytes the element is multipled by the position, so for arr[2], since an int is takes 4 bytes of storage, 4 * 2 = 8. It adds 8 from the base address to get to the element you want. 140732430094868 + 8 = 140732430094876, which checks out. Here are the calculations

arr[0] -> base address, offset 0, address: 140732430094868 -> 2
arr[1] -> offset 1, 1 * 4 = 4, address: 140732430094868 + 4 = 140732430094872 --> 5
arr[2] -> offset 2, 2 * 4 = 8, address: 140732430094868 + 8 = 140732430094876 -> 10
arr[3] -> offset 3, 3 * 4 = 12, address: 140732430094868 + 12 = 140732430094880 -> 7
arr[4] -> offset 4, 4 * 4 = 16, address: 140732430094868 + 16 = 140732430094884 -> 21

Here’s a link with some of the things I mentioned: How do we use offsets to calculate the address of any dimensional array and c++ - Why is the next element of an int array stored 4 bytes after the previous one? - Software Engineering Stack Exchange (I got the drawing from here, I just modified it. This might confuse you a bit so you might not want to look at it, I just wanted to credit where I got the drawing idea from.)

Hope this clarifies things! :slight_smile:

5 Likes