Pointer and array error-prone test questions analysis

remember

sizeof() calculates the size of the occupied memory space, and the unit is bytes. Don’t pay attention to what is stored in the memory.
sizeof is the operator
strlen() is
the length of the string calculated by the function strlen(). Essentially, the statistics are The meaning of the array name of the number of characters before \0'
:

  1. sizeof(array name), where the array name represents the entire array, and the calculation is the size of the entire array.
  2. &Array name, where the array name represents the entire array, and the address of the entire array is taken out.
  3. In addition, all array names represent the address of the first element.

error-prone

Example 1

char arr[] = {
    
    'a','b','c','d','e','f'};
printf("%d\n", strlen(arr));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));

1. arr represents the address of the first element. Since there is no '\0' as the end of the string, it will calculate backward until it encounters '\0', and print a random value. 2.* arr represents 'a', strlen(
) The parameter receives a character pointer, and the ascii value of 'a' is 97, accessing 97 as an address, which is an unknown address, will form an illegal access 3. &arr indicates the address of the entire array, and its type is
char * [6], when passed to strlen(), the parameter receives a char * address, then the array pointer will be cast into a character pointer, but the address of the entire array is also the address of the first element in value, but &arr + 1, skip the entire array, print random value
4.&arr+1, skip the entire array, and continue to visit until '\0' is encountered, so random values ​​will also be printed, this random value is the same as the first and third The printed random values ​​differ by 6

Example 2

char *p = "abcdef";
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p+1));
  1. p[0] can be parsed as * (p + 0), ie 'a', illegal access
  2. The &a stored in p, then &p is a secondary pointer, that is, the address of p, which is accessed backward from the address of p. Since the address of p is unknown, a random value will be printed
  3. &p+1 will skip the variable p, that is, skip 4 bytes, because p is a pointer, which occupies 4 bytes on a 32-bit platform, and will also print random values

Example 3

int a[3][4] = {
    
    0};
printf("%d\n",sizeof(a[3]));


Print 16, there are two reasons why there is no array out of bounds here
For example

int a = 10;
short s = 11;
printf("%d\n",sizeof(s = a + 2));
printf("%d\n",s);

1. An expression or value has two attributes, one is the value attribute and the other is the type attribute, and sizeof() calculates its type attribute, a is an integer, 2 is an integer, and it is truncated when put into a short. This expression The result is a short type, with a size of 2.
2. The test.c file we wrote needs to be compiled, and the link becomes test.exe to execute. The calculation of the expression also needs to be calculated in test.exe, but unfortunately The operation of sizeof has already been calculated during the compilation process, sizeof(s = a + 2)) directly becomes 2, s = a + 2 will not exist in test.exe, and of course it will not be calculated

Example 4

int arr[3][4];
printf("%d",sizeof(*arr + 1));

arr represents the address of the first element, that is, the address of the first line &a[0], the array name a[0] of the first line is dereferenced, and a[0] is not placed inside sizeof alone, indicating the address of the first element, that is, &a[ 0][0] , &a[0][0] +1 means &a[0][1]

Guess you like

Origin blog.csdn.net/wan__xia/article/details/129647202