Can we access an array as a pointer? Give an example – C interview question

Do you know that an array is basically a pointer to some sequential memory addresses in C?

If you don’t know this, join me to learn a cool C programming pointer trick!

In this article, I’ll show you how an array name acts like a pointer and how you can assign values using pointer notation.

This is a perfect example of pointers for beginners who want to understand arrays, pointers, and memory addresses in C.

C Program to access an array via pointer

To access an array as a pointer, just use the array name as a pointer. To access an element of an array as a pointer, just use the index and add it to the array name or array’s base address.

Let me show this to you by writing a C program and tell me what will be printed in place of the %d in the printf statement:

#include <stdio.h>

int main(){
    int arr [10];

    *arr = 100;
    *(arr + 3) = 300;

    printf("Arr & arr+3 = %d and %d", arr[0], arr[3]);

    return 0;
}

Here are your options:

A. 0 and 0
B. 100 and 300
C. Compilation Error
C. Segmentation Error (Runtime error)

Write the correct option in the comment box and then copy the code and execute in our online C compiler to see what output it gives.

The answer is B. 100 and 300. Let me explain you why does this happen.

TL;DR

Look at this shorts video which explains the reason of this behavior.

Explanation of the array’s behavior

When you declare an array, the array name is called as the base address and the whole array occupies sequentially on the memory.

In the above code arr [10], arr is the name of the array and this is also the base address of the array.

Let’s look at the below array as a sequence of memory addresses:

                        arr     arr+1    arr+2   arr+3   arr+4   arr+5   arr+6   arr+7    arr+8  arr+9

Address →     1000   1004    1008    1012    1016    1020    1024    1028    1032   1036
                   +------+------+------+------+------+------+------+------+------+------+
Element →   |   100  |   - -   |    --    |  300   |   - -    |   - -    |   - -   |   - -    |   - -    |   - -   |
                   +------+------+------+------+------+------+------+------+------+------+
Index   →          0          1           2          3         4        5           6          7         8         9

This base address (arr) points to the 0th element of the array and when this is access as a pointer, it can access the 0th element directly.

So, when I did *arr = 100 it stores the value of 100 in the 0th element which is accessed via pointer dereferencing.

This same value can also be accessed via indexing such as arr [0] = 100. Both are the same thing.

Similarly, as I have added above, the arr+3 becomes the address of 3rd element. Hence when wrote the code *(arr + 3) = 300 it actually writes 300 at the 3rd location (assuming we start from 0th element) of the array.

By doing so you can access any array element as a pointer variable.

Conclusion

Learning a new thing is cool but you should also know where do you use this concept in real life.

This kind of data access is done to access memory mapped hardware registers if they are sequential.

A PCI device has a configuration address space which is a sequence of 256 bytes of registers which is divided into 16 integer type registers or an array of 16 integers.

When you write device driver code for such a PCI device, you have to take the base address as the address of array and then the index is called as an offset via which you can access the whole address space.

In my example, you can call the 0 and 3 as the offset.

That’s all for now in this example.

If you have any doubts on concepts of pointers or arrays in C, then you can ask it on the topic specific forum page.

Visit our online C compiler tool which makes it pretty easy for you to run any kind of C code and learn it quicker from your browser instead of running a compiler applciation.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.