C/C++ pointer and array written test questions analysis

Personal homepage : Click me to enter the homepage

Column classification: C language elementary       C language programming——KTV        C language mini-game      C language advanced

C language test questions

Everyone is welcome to like, comment and collect.

Work hard together and go to a big factory together.

Table of contents

 

1 Introduction 

2. One-dimensional array

2.1 Exercise 1

2.2 Exercise 2

2.3 Exercise 3

3. Two-dimensional array

4. Summary


 

1 Introduction 

        Earlier, we learned the basics of pointers and advanced pointers. If you want to learn again, you can click Pointer Advanced  , Pointer Advanced , and Pointer Beginner . This time, I mainly study the written test questions on pointers with you. This writing question is very interesting. It uses the sizeof function and strlen function. Next, let us feel the fun of these questions. We need to know when we are doing the questions

  • sizeof (array name) here is to calculate the size of the entire array, that is, the number of bytes occupied.
  • &array name gets the address of the entire array
  • The remaining array names are the addresses of the first elements.

2. One-dimensional array

2.1 Exercise 1

#include<stdio.h>
int main()
{
	int a[] = { 1,2,3,4 };
	printf("%d\n", sizeof(a));
	printf("%d\n", sizeof(a + 0));
	printf("%d\n", sizeof(*a));
	printf("%d\n", sizeof(a + 1));
	printf("%d\n", sizeof(a[1]));
	printf("%d\n", sizeof(&a));
	printf("%d\n", sizeof(*&a));
	printf("%d\n", sizeof(&a + 1));
	printf("%d\n", sizeof(&a[0]));
	printf("%d\n", sizeof(&a[0] + 1));
	return 0;
}

        In sizeof(a), a is the array name and the size of the array is calculated, so it is 16 bytes; in sizeof(a+0). The array name a does not exist alone, so it is the address of the first element. Since it is an address, Then it occupies 4/8 bytes, sizeof(*a), the array name does not exist alone, *a is the first element, occupying 4 bytes, sizeof(a+1), the array name does not exist alone, a is the address of the first element , a+1 is the address of the second element, which is the address and occupies 4/8 bytes. sizeof(a[1]) is the second element. The element is int and occupies 4 bytes. sizeof(&a), It is the address of the array, belongs to the address, and occupies 4/8 bytes, sizeof(*&a), we know that * and & can be offset when put together, so it is equivalent to sizeof(a), so it occupies 16 bytes, sizeof(&a +1), &a is the address of the array, adding 1 means adding 16 bytes, but it is the address, so it occupies 4/8 bytes, sizeof(&a[0]), is the address of the first element, occupies 4 /8 bytes, sizeof(&a[0]+1) is the address of the second element, occupying 4/8 bytes.

2.2 Exercise 2

#include<stdio.h>
#include <string.h>
int main()
{
	char arr[] = { 'a','b','c','d','e','f' };
	printf("%d\n", sizeof(arr));
	printf("%d\n", sizeof(arr + 0));
	printf("%d\n", sizeof(*arr));
	printf("%d\n", sizeof(arr[1]));
	printf("%d\n", sizeof(&arr));
	printf("%d\n", sizeof(&arr + 1));
	printf("%d\n", sizeof(&arr[0] + 1));


	printf("%d\n", strlen(arr));
	printf("%d\n", strlen(arr + 0));
	printf("%d\n", strlen(*arr));
	printf("%d\n", strlen(arr[1]));
	printf("%d\n", strlen(&arr));
	printf("%d\n", strlen(&arr + 1));
	printf("%d\n", strlen(&arr[0] + 1));
	return 0;
}

         sizeof(arr), arr exists alone, so it is 6 bytes, sizeof(arr+0), because the array name does not exist alone, it belongs to the first element address, belongs to the address, and occupies 4/8 bytes. sizeof(*arr), the array name does not exist alone. *arr is the first element, which is a character type and occupies 1 byte. sizeof(arr[1]), is the second element and occupies 1 byte. sizeof(&arr), is the address of the array, belongs to the address, occupies 4/8 bytes, sizeof(&arr+1), &arr is the address of the array, &arr+1 skips this array, but it is still an address, belongs to the address, It occupies 4/8 bytes, sizeof(&arr[0]+1), &arr[0] is the address of the first element, &arr[0]+1 is the address of the second element, which is an address and occupies 4/8 words. Festival. For strlen(arr), the array name exists alone, but we don’t know when \0 appears, so it is a random value. For strlen(arr+0), we get the first address, but we don’t know when \0= appears, so It is a random value. *a and arr[1] in strlen(*a) and strlen(arr[1]) are both characters. Using ASCLL for conversion will perform integer promotion and zero padding, because the computer will regard it as an address, but Since this address has not been initialized, that is to say, it is a wild pointer, which will cause computer errors and program crashes. For the last three games, they are all random values ​​because there is no \0.

2.3 Exercise 3

#include<stdio.h>
#include <string.h>
int main()
{
	char arr[] = "abcdef";
	printf("%d\n", sizeof(arr));
	printf("%d\n", sizeof(arr + 0));
	printf("%d\n", sizeof(*arr));
	printf("%d\n", sizeof(arr[1]));
	printf("%d\n", sizeof(&arr));
	printf("%d\n", sizeof(&arr + 1));
	printf("%d\n", sizeof(&arr[0] + 1));


	printf("%d\n", strlen(arr));
	printf("%d\n", strlen(arr + 0));
	printf("%d\n", strlen(*arr));
	printf("%d\n", strlen(arr[1]));
	printf("%d\n", strlen(&arr));
	printf("%d\n", strlen(&arr + 1));
	printf("%d\n", strlen(&arr[0] + 1));
	return 0;
}

        For sizeof(arr), the array name exists alone, so it is 6 bytes. sizeof(arr+0), the array name does not exist alone. It is the address of the first element, which belongs to the address and occupies 4/8 bytes. sizeof( *arr), the array name does not exist alone and is the first element. The first element is of type char and occupies 1 byte. sizeof(arr[1]), is the second element of the array, which is char type and occupies 1 byte. sizeof(&arr) and sizeof(&arr+1), &arr is the address of the array, &arr+1 is the address behind the array, which belongs to the address and occupies 4/8 bytes, sizeof(&arr[0]+1), &arr[ 0] is the address of the first element, &arr[0]+1 is the address of the second element, and the address occupies 4/8 bytes. For strlen(arr), the array name exists alone, so it is 6. For strlen(arr+0), arr does not exist alone, and it belongs to the address of the first element, so it is 6 bytes. *a and arr[1] in strlen(*a) and strlen(arr[1]) are both characters. Using ASCLL for conversion will perform integer lifting and zero padding, because the computer will regard it as an address, but because of this address It is not initialized, which means it is a wild pointer, which will cause computer errors and program crashes. For the last three, &arr gets the address of the array, &arr[0]+1 gets the address of the second element, and &arr+1 gets the random value from the address behind the array.

3. Two-dimensional array

#include<stdio.h>
#include <string.h>
int main()
{
	int a[3][4] = { 0 };
	printf("%d\n", sizeof(a));
	printf("%d\n", sizeof(a[0][0]));
	printf("%d\n", sizeof(a[0]));
	printf("%d\n", sizeof(a[0] + 1));
	printf("%d\n", sizeof(*(a[0] + 1)));
	printf("%d\n", sizeof(a + 1));
	printf("%d\n", sizeof(*(a + 1)));
	printf("%d\n", sizeof(&a[0] + 1));
	printf("%d\n", sizeof(*(&a[0] + 1)));
	printf("%d\n", sizeof(*a));
	printf("%d\n", sizeof(a[3]));
	return 0;
}

        For sizeof(a), the array name exists alone and is the address of the array, so it is 4*3*4=48 bytes. For sizeof(a[0]), this is special. First of all, it exists alone. We can Think of a two-dimensional array as an array of one-dimensional arrays. a[0] is the address of the first row, so it is 4/8 bytes. For sizeof(a[0]+1), a[0] is one-dimensional. The array name of the array, because a[0] is not alone in sizeof(), is the address of the first element of the one-dimensional array, occupying 4/8 bytes, then sizeof(*(a[0]+1)) is The second element occupies 4 bytes. a is the array name of the two-dimensional array. a does not exist alone in sizeof(a+1), so it is the first element of the two-dimensional array, which is the address of the first row, a +1 is the address of the second line, accounting for 4/8 bytes (for two-dimensional arrays, we can think of two-dimensional arrays as arrays of one-dimensional arrays, such as arr[3][4], arrays of two-dimensional arrays Named arr, the array of one-dimensional array is named arr[3]). For sizeof(*(a+1)), a is the array name of the two-dimensional array. It does not exist alone, so it is the first element of the two-dimensional array. That is, the address of the first row, a+1 is the address of the second row * (a+1) is all the elements of the second row, occupying 16 bytes. sizeof(&a[0]+1), a[0] is the array name of the one-dimensional array, &a[0] is the address of the first row, and &a[0]+1 is the address of the second row. , the address occupies 4/8 bytes, and *dereferencing it occupies 16 bytes. For sizeof(a[3]), although the array has overflowed, it is still 16 bytes, because the function will not When you actually enter, it is the same as a[0].

4. Summary

        After reading these questions, I can summarize it as;

  • sizeof(array name), where array name is the entire array.
  • & array name plus and minus integers where & array name is the address of the entire array. even in sizeof() is the address of the entire array
  • For a two-dimensional array, we can regard the two-dimensional array as an array of one-dimensional arrays, such as arr[3][4], the array of the two-dimensional array is named arr, and the array of the one-dimensional array is named arr[3],&arr, arr is the address of a two-dimensional array, but in sizeof, arr plus an integer indicates the address of the row, arr[integer] & arr[integer] both indicate the address of the row.

That’s the end of today’s content. I hope you can connect three times with one click.

 

Guess you like

Origin blog.csdn.net/Infernal_Puppet/article/details/133020826