The Relationship Between Arrays and Pointers in C Language – Part Four

5.Why Create the Array Data Structure

From the previous analysis, we can understand that the essence of an array name is a pointer. Since we already have the pointer data type, why create the array data type? Isn’t that redundant? Can we program normally without creating arrays? The answer is: Yes, it is indeed possible to program without arrays. To understand this issue, let’s take a look at another program:

#include

#include

#include

int main() {

inti7;

inti6;

inti5;

inti4;

inti3;

inti2;

inti1;

inti0;

int* alias;

char* pointor_char;

intn;

printf(“Enter the number for array array_t:”);

scanf(“%d”,&n);

intarray_t[n];

intarray_r[5];

alias= &i0;

printf(“\narray_t is%p\n”, array_t);

printf(“array_t+ 1 is %p\n”, array_t + 1);

printf(“array_r is%p\n”, array_r);

printf(“array_r+ 1 is %p\n”, array_r + 1);

printf(“alias is%p\n”, alias);

printf(“\n&i0 is%p\n”, &i0);

printf(“&i1 is%p\n”, &i1);

printf(“&i2 is%p\n”, &i2);

printf(“&i3 is%p\n”, &i3);

printf(“i0 is%d\n”, i0);

printf(“i1 is%d\n”, i1);

printf(“i2 is%d\n”, i2);

printf(“i3 is%d\n”, i3);

(&i0)[2]= 100;

(&i0)[3]= 255;

printf(“\nAfterset value to i2 and i3\n”);

printf(“i0 is%d\n”, i0);

printf(“i1 is%d\n”, i1);

printf(“i2 is%d\n”, i2);

printf(“i3 is%d\n”, i3);

printf(“alias[0] is%d\n”, alias[0]);

printf(“alias[1] is%d\n”, alias[1]);

printf(“alias[2] is%d\n”, alias[2]);

printf(“alias[3] is%d\n”, alias[3]);

printf(“(&i0)[0] is%d\n”, (&i0)[0]);

printf(“(&i0)[1] is%d\n”, (&i0)[1]);

printf(“(&i0)[2] is%d\n”, (&i0)[2]);

printf(“(&i0)[3] is%d\n”, (&i0)[3]);

printf(“\nsizeof(array_t) is%d\n”, sizeof(array_t));

return0;

}

The output of the compiled program is as follows:

Enter the number for array array_t:15

array_t is 0x7fffe74f43b0

array_t + 1 is 0x7fffe74f43b4

array_r is 0x7fffe74f4400

array_r + 1 is 0x7fffe74f4404

alias is 0x7fffe74f4420

&i0 is 0x7fffe74f4420

&i1 is 0x7fffe74f4424

&i2 is 0x7fffe74f4428

&i3 is 0x7fffe74f442c

i0 is 16

i1 is 0

i2 is 8388608

i3 is 0

After set value to i2 and i3

i0 is 16

i1 is 0

i2 is 100

i3 is 255

alias[0] is 16

alias[1] is 0

alias[2] is 100

alias[3] is 255

(&i0)[0] is 16

(&i0)[1] is0

(&i0)[2] is100

(&i0)[3] is255

sizeof(array_t) is 60

Analyzing the goals of the above program, we can see that when a program needs to use a collection of data of the same type, and there is a certain rule regarding the order of storage space, we can have many implementation methods, all of which can achieve the goal. Whether using i0, i1, … or (&i0)[0], (&i0)[0], … or alias[0], alias[1], … or *alias, *(alias + 1), *(alias + 2), … the results are all the same. However, if we take a closer look at these implementation methods, we will find that using the array data object is the simplest and clearest. For example, if you need to declare an array variable with hundreds or even thousands of elements, at least the variable declaration can greatly simplify the developer’s workload. The creation of the array data type is to achieve this purpose.

In fact, the array operator [] is equivalent to the combination of the operators * and () along with the operator +. The previous (&i0)[0] is a transformation of the operator combination *(+), where the parentheses in (&i0) are added to change the precedence. In programming, you can completely use the array member access operator instead of pointer operations. For example, when accessing a pointer object, you can directly use point[0] instead of *point. There is no problem with this, depending on personal preference.

At this point, let’s revisit a previous statement: basic data type variables can be viewed as an array variable containing only one array element. However, to understand this concept, it is best to clarify another concept: almost all basic data type variables are essentially pointers, but the compiler defaults to choosing to reference that variable in semantic understanding. In the following series, we will also find opportunities to analyze this concept.

Finally, if you are interested, it is recommended to carefully interpret the several small programs provided in the article. Although these small programs are very simple, they can provide a deep understanding of arrays and pointers. If you can truly understand the array and pointer operations in these programs, it will greatly help you understand the concepts of pointers and arrays.

Leave a Comment