Deep understanding of c arrays

  • 2020-04-02 02:06:00
  • OfStack

What is the name of an array

An array is a contiguous block of memory.
Like declaring an int array

An int array [] = {1, 2, 3};

What does array represent? Some sources say that the array name is a constant pointer to the first address of the array.

Now we can verify that.
I know that the sizeof operator can return the number of bytes in memory occupied by an object or type.
Such as:
Int I = 1;
So the result of sizeof of I is going to be 4.

So let's print sizeof of array.


Printf (" % d \ n ", sizeof (array));

The result: 12.

But we all know that sizeof ==4.


So we get: An array name is not exactly a constant pointer to the first address of an array .

Why use all of them? Because when we use arrays to access array elements. It becomes like a constant pointer again.

Such as
Array [0] is the same thing as *(array+0).

In this case, array is a constant pointer to the first address of the array, and the pointer type is a pointer to the array element type. So this is int star

We can understand it this way:

The name of a university is array.
Someone asks you what array is. You're going to tell him that array is the university, the area, whatever.
But if someone asks you how to get to array, you'll tell them where the gate is.

Conclusion: the array name actually represents a memory region, but when used it becomes a constant pointer to the first address of the array.

But here's a little pitfall:


#include <stdio.h>
void foo(int a[])
{
  printf("%dn",sizeof(a));
}
int main(void)
{
  int array[]={1,2,3};
    foo(array);
    return 0;
}

Instead of a 12, it's a 4.

For efficiency reasons, array arguments are referenced rather than copied. Because the length of the array can be large, it takes too much resources to make a copy.
Although I'm like this the function looks like this


void foo(int a[])
{
printf("%dn",sizeof(a));
}

This is what the compiler thinks


void foo(int *a)
{
printf("%dn",sizeof(a));
}

So sizeof of a is sizeof must be 4;

Two character array

First let's look at a simple program


# include <stdio.h>
int main(void)
{
  char *str1="abc";
  char str2[]="def";
  printf("%sn",str1+4);
  return 0;
}

The output is def.


Note that whenever "XXXXX" is used in c, the system will automatically add the contents of double quotes to the character constant area.
Note: printf (" XXXX "); "XXXX" will not be added to the character constant area.


char *str1="abc";         //ABC 0 is added to the character constant and the first address is assigned to the STR pointer variable.
char str2[]="def";        //Def 0 is added to the character constant area, and a character array is added to the function stack to also def0, str2 points to the array in the stack.
char str[]={'x','y','z'}; //Only arrays are added to the function stack

Because the character constant area is continuous, so

Printf (" % s \ n ", str1 + 4);


You can print the value of str2.

Three dimensional array

An int array [], [3] = {6};

As we said earlier, when accessing an element using array, an array is simply a pointer to the element of the array, to the first address of the array.
The elements of a two-dimensional array are arrays,

It's easier to write:
An int array [], [3] = {{1, 2, 3}, {4 and 6}};

So you could view an array that way
Int (* const array) [3];
When I access an array element


Array [x][y] to the compiler is *(*(array+x)+y)

*(array+x) returns an array of type "int[3]" on line x,

The array name is used as the first address pointer when accessing an element, where *(array+x) is equal to the array name,
Pointer int * to array+sizeof(int(*)[3])*x.

When accessing the y th element of the array, we use *(*(array+x)+y).

These are my understanding of c language array, if the wrong place, thanks for correcting, light puff-puff-puff-puff-puff-puff-puff-puff-puff-puff-puff-puff-puffs.


Related articles: