Operations on arrays (1)

Preparation

1.sizeof
sizeof is a monocular operator that calculates the data type or variable length (that is, the number of bytes taken)
int integer type takes up 4 bytes; char character type takes up 1 byte; address takes up 4 bytes
2.strlen
strlen (address), a function that calculates the length of a string with '\ 0' as the end flag (excluding '\ 0')
3. & + array name, array name represents the whole array, here is the address of the whole array
sizeof (array name). The array name represents the whole array. It calculates the size of the whole array, and the unit is byte
In addition to the above two cases, all array names encountered represent the address of the first element of the array

One dimensional integer array

int main()
{
    int a[] = { 1, 2, 3, 4 };
    printf("%d\n", sizeof(a));//16---a represents the whole array, and calculates the size of the whole array (in bytes)
    printf("%d\n", sizeof(a+0));//4---a+0 indicates the address of the first element
    printf("%d\n", sizeof(*a));//4---a is the address of the first element, * dereference is the first element
    printf("%d\n", sizeof(a+1));//4---a+1 indicates the address of the second element
    printf("%d\n", sizeof(a[1]));//4---a[1] indicates the second element
    printf("%d\n", sizeof(&a));//4 ---- a represents the address of the array, but the length of the address is 4
    printf("%d\n", sizeof(*&a));//16 - & A represents the address of the array, * dereference represents the size of the entire array
    printf("%d\n", sizeof(&a+1));//4 ---- a represents the address of the array plus 1, skipping the address of the entire array
    printf("%d\n", sizeof(&a[0]));//4 --- address of the first element
    printf("%d\n", sizeof(&a[0] + 1));//4 --- the address of the first element plus + 1, that is, the address of the second element
    system("pause");
    return 0;
}

Character array

int main()
{
    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    printf("%d\n", sizeof(arr));//6---arr represents the whole array size
    printf("%d\n", sizeof(arr + 0));//4---arr+0 indicates the address of the first element
    printf("%d\n", sizeof(*arr)); //1 - * arr represents the first element
    printf("%d\n", sizeof(arr[1]));//1---arr[1] indicates the second element
    printf("%d\n", sizeof(&arr));//4 ---- arr represents the address of the array
    printf("%d\n", sizeof(&arr + 1));//4 ---- arr represents the address of the array plus 1, skipping the address of the entire array
    printf("%d\n", sizeof(&arr[0] + 1));//4 - & arr [0] first element address plus + 1, that is, the second element address
    system("pause");
    return 0;
}
int main()
{
    char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
    printf("arr==%d\n", strlen(arr));//Random value--- arr Represents the first element address, but an array arr Not in'\0'End flag, when strlen()Function from'a'Count to'f'It's always counting backwards
    printf("arr+0==%d\n", strlen(arr + 0));//Random value---Represents the first element address
    //printf("%d\n", strlen(*arr));//error code---*arrRepresents the first element, but strlen The argument to the function is the address
    //printf("%d\n", strlen(arr[1]));//error code
    printf("&arr==%d\n", strlen(&arr));//Random value---Represents the address of the array, but it is the same as the address of the first element in value
    printf("&arr+1==%d\n", strlen(&arr + 1));//Random value---&arr Represents the address plus of an array1,Skip the address of the entire array (its value is equal to strlen(arr)difference6)
    printf("&arr[0]+1==%d\n", strlen(&arr[0] + 1));//Random value---&arr[0]Address plus of first element+1,That is, the address of the second element (its value is equal to strlen(arr)difference1)
    system("pause");
    return 0;
}

int main()
{
    char arr[] = "abcdef";
    printf("%d\n", sizeof(arr));//7---arr represents the whole array
    printf("%d\n", sizeof(arr + 0));//4 --- address of the first element of the array
    printf("%d\n", sizeof(*arr));//1 --- first element of array
    printf("%d\n", sizeof(arr[1]));//1 --- array first element
    printf("%d\n", sizeof(&arr));//4 --- address of array
    printf("%d\n", sizeof(&arr + 1));//4 ---- add 1 to the address of the array, skip the address of the whole array
    printf("%d\n", sizeof(&arr[0] + 1));//4 --- the address of the first element plus + 1, that is, the address of the second element
    system("pause");
    return 0;
}
int main()
{
    char arr[] = "abcdef";
    printf("%d\n", strlen(arr));//6---arr Represents the address of the first element of the array
    printf("%d\n", strlen(arr + 0));//6---arr+0Represents the address of the first element of the array
    //printf("%d\n", strlen(*arr));//error code
    //printf("%d\n", strlen(arr[1]));//error code
    printf("%d\n", strlen(&arr));//6---Represents the address of the array, but it is the same as the address of the first element in value
    printf("%d\n", strlen(&arr + 1));//Random value---Array address plus1,Skip address of entire array
    printf("%d\n", strlen(&arr[0] + 1));//5---&arr[0]Address plus of first element+1,The address of the second element
    system("pause");
    return 0;
}
int main()
{
    char *p = "abcdef";
    printf("%d\n", sizeof(p));//4---p means the address where the pointer variable holds' a '(the pointer variable is the address)
    printf("%d\n", sizeof(p + 1));//4---p+1 is the address of 'b'
    printf("%d\n", sizeof(*p));//1---p refers to the first address of the character, * dereference means' a '
    printf("%d\n", sizeof(p[0]));//1---p[0] represents the first element of string
    printf("%d\n", sizeof(&p));//4 ---- P represents the address of pointer variable p
    printf("%d\n", sizeof(&p + 1));//4 --- skip address with a pointer address size (i.e. four bytes)
    printf("%d\n", sizeof(&p[0] + 1));//4 --- add + 1 to the address of the first element of the string, that is, the address of the second element

    printf("%d\n", strlen(p));//6---p refers to the address of the first element of the string, that is, calculating the whole string length (excluding '\ 0')
    printf("%d\n", strlen(p + 1));//5 --- add + 1 to the address of the first element of the string, that is, add + 1 to the address of the second element, that is, calculate the length of the string starting from the second character
    //Printf (""% d \ n ", strlen (* P)); / / error code
    //Printf (""% d \ n ", strlen (P [0]))); / / error code
    printf("%d\n", strlen(&p));//Random value -- & p represents the address of pointer variable p. if there is no required string (i.e. no '\ 0') function, the value will end arbitrarily
    printf("%d\n", strlen(&p + 1));//Random value --- skip the address with a pointer address size (i.e. four bytes). The function with no required string (i.e. no '\ 0') will end arbitrarily
    printf("%d\n", strlen(&p[0] + 1));//5 --- add + 1 to the address of the first element of the string, that is, add + 1 to the address of the second element, that is, calculate the length of the string starting from the second character
    system("pause");
    return 0;
}

Two-dimensional array

int main()
{
    int a[3][4] = { 0 };          
    printf("%d\n", sizeof(a));//48---a indicates the size of the whole array
    printf("%d\n", sizeof(a[0][0]));//4 --- the first element in the first line (integer)
    printf("%d\n", sizeof(a[0]));//16---a[0] is the first row array name, that is, the size of the first row array
    printf("%d\n", sizeof(a[0] + 1));//4 --- a[0] in a[0] + 1 indicates the address of the first element in the first line, and 1 is the address of the second element in the first line
    printf("%d\n", sizeof(a + 1));//4 --- in a + 1, a represents the address of the element in the first line, and 1 is the address of the second line
    printf("%d\n", sizeof(&a[0] + 1));//4 - & A [0] indicates the address of the first line element plus 1, that is, the address of the second line element
    printf("%d\n", sizeof(*a));//16 --- the address of the first line element in a, * dereference is the size of the first line element
    printf("%d\n", sizeof(a[3]));//16---a[3] is the array name, that is, the size of the array in the first row (although a[3] is out of bounds, it is not calculated in sizeof)
    system("pause");
    return 0;
}

Posted by Mehdi on Tue, 31 Mar 2020 18:40:07 -0700