Inside C array

Today we are going to talk a bout C array, and memory and we are going to test an unusual array style.

First of all, lets talk about memory in C, there are two types of data memory in C :

  1. Stack memory.
  2. Heap memory.

Data allocated in the stack is automatically reclaimed upon exiting the function from which it was called. In contrast, heap-allocated data requires manual freeing. Let's examine an example to illustrate this. :


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int stackData(int *a, int *b)
{
    return *a+*b;
}

int heapData(int *pa, int *pb)
{
    int ans = *pa+*pb;
    free(pa);
    free(pb);
    pa = NULL;
    pb = NULL;
    return ans;
}

The first function which called stackData receives two address a and b of type integer and return the sum of the values inside these locations.  

The second function heapData also receives two pointers but those pointer are allocated dynamically. Basically dynamic allocation its an operation that  reserve  the memory to be grow and shrink dynamically.  after the the function calculated the sum it will store the result in ans which is an integer variable which allocated in the stack region, then the pa and pb will be freed and pointed to the NULL address, which means that other programs can use that freed address. The ans memory location will be freed aromatically after the program exit the function and only a copy of the ans value will be returned. It's crucial to understand that all user programs are managed by the operating system (OS). 

When a program needs to allocate memory, it must request this allocation from the OS. This means that the program communicates with the OS to obtain the necessary memory space for its operations. The OS then manages this allocated memory, ensuring proper allocation and deallocation as required by the program. This interaction between the program and the OS for memory management is essential for the efficient and effective functioning of computer systems. However for bare-metal programs that interact directly with the hardware the compiler needs to know the  memory size in advance we will discuss this in other post.

Now let's take this example:


#include <stdio.h>
#include <string.h>
int main()
{
    char arr[]="DigitalZoul";
    for(int i=0;i<strlen(arr);i++)
    {
        printf("%c",arr[i]);
    }
    return 0;
}

The provided code will output the string "DigitalZoul". Here, the variable arr is allocated in the stack region. The name of the array itself acts as a pointer to the first element in the array. When we use the statement arr[i], we are dereferencing the value inside the i-th location of the array, allowing us to access individual elements of the array.

 Now we have another code :


#include <stdio.h>
#include <string.h>
int main()
{
    char arr[]="DigitalZoul";
    for(int i=0;i<strlen(arr);i++)
    {
        printf("%c",i[arr]);
    }
    return 0;
}

Give this code a try, and you might be surprised by the result! You're probably wondering how this code even functions. To answer this question we need to understand how the arr[i] works.

equation

Essentially, arr[i] is equivalent to *(arr+i). Since the operation inside the brackets is an addition operation, we know that (arr+i) is the same as (i+arr), following the associative law. Consequently, *(arr[i]) is equivalent to *(i[arr]).