The essence analysis of 27 μ array

Keywords: C Programming

The concept of array

  • Array is the order of variables of the same type

Size of array

  • Array stores data in a continuous memory space
  • The number of array elements can be displayed or implicitly specified

int a[5] = {1, 2};
int b[] = {1, 2};

Question:

What is the value of a[2], a[3], a[4]?
How many elements does b contain?

Programming experiment: initialization of array

#include <stdio.h>

int main()
{
    int a[5] = {1, 2};
    int b[] = {1, 2};
    
    printf("a[2] = %d\n", a[2]);
    printf("a[2] = %d\n", a[3]);
    printf("a[2] = %d\n", a[4]);
    
    printf("sizeof(a) = %d\n", sizeof(a));
    printf("sizeof(b) = %d\n", sizeof(b));
    printf("count for a: %d\n", sizeof(a)/sizeof(*a));  // sizeof(*a) ==> sizeof(a[0]) ==> sizeof(int)
    printf("count for b: %d\n", sizeof(b)/sizeof(*b));  // sizeof(*b) ==> sizeof(b[0]) ==> sizeof(int)
    
    return 0;
}
Output:
a[2] = 0
a[2] = 0
a[2] = 0
sizeof(a) = 20
sizeof(b) = 8
count for a: 5
count for b: 2

Array address and array name

  • Array name represents the address of the first element of the array
  • The address of an array needs to be obtained by taking the address character
  • The address of the first element of the array is the same as the address value of the array
  • The address of the first element of an array and the address of an array are two different concepts

Programming experiment: array name and array address

#include <stdio.h>

int main()
{
    int a[5] = { 0 };
    
    printf("a = %p\n", a);
    printf("&a = %p\n", &a);
    printf("&a[0] = %p\n", &a[0]);
    
    printf("\n");
    
    printf("sizeof(a) = %d\n", sizeof(a));
    printf("sizeof(a[0]) = %d\n", sizeof(a[0]));
}
Output:
a = 0xbf8bb66c
&a = 0xbf8bb66c
&a[0] = 0xbf8bb66c

sizeof(a) = 20
sizeof(a[0]) = 4

Address has two meanings: starting address and length

Array a starting address: & A Length of array A: sizeof(a) = 20
Array a[0] starting address: & a[0] Array a[0] length: sizeof(a[0]) = 4

Blind spot of array name

  • Array name can be regarded as a constant pointer
  • Array name refers to the starting address of the first element of the array in memory
  • The array name does not contain the length information of the array
  • Array names can only be used as right values in expressions
  • Array names cannot be treated as constant pointers only in the following cases

    • Array name as parameter of sizeof operator
    • Array name as parameter of & operator

##Example analysis: array and pointer are not the same

#include <stdio.h>

int main()
{
    int a[5] = {0};
    int b[2];
    int* p = NULL;
    
    p = a;
    
    printf("a = %p\n", a);
    printf("p = %p\n", p);
    printf("&p = %p\n", &p);
    printf("sizeof(a) = %d\n", sizeof(a));
    printf("sizeof(p) = %d\n", sizeof(p));
    
    p = b;
    
    printf("b = %p\n", b);
    printf("p = %p\n", p);
    printf("&p = %p\n", &p);
    printf("sizeof(b) = %d\n", sizeof(b));
    printf("sizeof(p) = %d\n", sizeof(p));
}
Output:
a = 0xbf8aebe0
p = 0xbf8aebe0
&p = 0xbf8aebfc
sizeof(a) = 20
sizeof(p) = 4
b = 0xbf8aebf4
p = 0xbf8aebf4
&p = 0xbf8aebfc
sizeof(b) = 8
sizeof(p) = 4

Summary

  • Array is a continuous memory space
  • The address of the array and the address value of the first element of the array are the same, but they have different meanings
  • Array names are treated as constant pointers in most cases
  • Array name is not a pointer. It cannot be equated with a pointer

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Posted by SnakeO on Fri, 06 Dec 2019 14:51:13 -0800