Analysis of pointers and arrays of C language practice (1.2)

Foreword:

The book continues from the previous chapter, let's continue with today's study! Not much nonsense, let's code the content of the character array!

char *p is a character pointer, * means that p is a pointer, and char means that the object type pointed to by p is char type!

char*p="abcdef";

 When we store the address of the string abcdef in p, does p store the entire string or just the address of a? It's time to debug the code

At this point we will find that the address in p is also the address of a in the string ! Let's keep learning

int main()
{
	char* p = "abcdef";
	printf("%d\n", sizeof(p));
	printf("%d\n", sizeof(p + 1));
	printf("%d\n", sizeof(*p));
	printf("%d\n", sizeof(p[0]));
	printf("%d\n", sizeof(&p));
	printf("%d\n", sizeof(&p + 1));
	printf("%d\n", sizeof(&p[0] + 1));
	printf("%d\n", strlen(p));
	printf("%d\n", strlen(p + 1));
	printf("%d\n", strlen(*p));
	printf("%d\n", strlen(p[0]));
	printf("%d\n", strlen(&p));
	printf("%d\n", strlen(&p + 1));
	printf("%d\n", strlen(&p[0] + 1));
	return 0;
}

start analysis

printf("%d\n", sizeof(p));

p stores the address of a, sizeof(p) asks for the size of the address, and the size of the address is 4/8 bytes

printf("%d\n", sizeof(p + 1));

p points to the address of a in the string, p+1 points to the address of b in the string, sizeof(p+1) finds the size of the address, and the size of the address is 4/8 bytes

printf("%d\n", sizeof(*p));

 p points to the address of a in the string, *p refers to a, sizeof(*p)-->sizeof(a), so the size is 1 byte

printf("%d\n", sizeof(p[0]));

p[0]-->*(p+0)-->*(p), p stores the address of a, so *p points to a, sizeof(p[0]) calculates sizeof(a) The size of the result is 1

printf("%d\n", sizeof(&p));

The address of the string is stored in p, the type of p is char*, p itself also has an address, its type is char**! So sizeof(&p) also calculates the size of the address, and the result is 4/8

 Next, let's analyze the next code.

printf("%d\n", sizeof(&p + 1));

&p takes out the address of p and adds 1. We don’t know what the specific address is, but one thing is certain is that sizeof(&p+1) calculates the size of the address, and the address size is 4/8 bytes size

printf("%d\n", sizeof(&p[0] + 1));

&(*p+0)-->p address, &p[0]+1 gets the address of b in the string, so at this time sizeof calculates the size of the address, and the address size is 4/8 bytes. By debugging the code, it can be found that it is indeed the address of b

Next we use strlen, the code! Remember the strlen feature? Calculate the number of characters before encountering \0, the string will hide a \0

printf("%d\n", strlen(p));

p still points to the address of a in the string, and strlen starts from the address of a until encountering \0 during calculation , then the calculation result at this time is 6 bytes

printf("%d\n", strlen(p + 1));

p still points to the address of a in the string, so p+1 points to the address of b in the string. At this time, strlen starts to calculate from the address of b until encountering \0, and the calculation result is 5.

printf("%d\n", strlen(*p));

p still points to the address of a in the string, what *p gets is a, remember what I said in the previous article, the parameter required by strlen is the address of const char* type instead of the character a, so strlen(*p ) The result is error

printf("%d\n", strlen(p[0]));

 p[0]-->*(p+0)-->*p-->a, the explanation is the same as above, and the result is error

printf("%d\n", strlen(&p));

 Although strlen(&p) also calculates the address, we don't know when \0 will be encountered, so the result at this time is a random value

printf("%d\n", strlen(&p + 1));

strlen(&p+1) Even if you add 1 to the line, you still don’t know when you will encounter \0, and the result is still a random value

printf("%d\n", strlen(&p[0] +1)

&p[0]-->&*(p+0)-->p, p stores the address of a, so &p[0]+1 points to the address in string b at this time, strlen(&p[0 ]+1) results in 5 bytes

Ok, after the analysis is complete, let’s run the code and have a look. Of course, if the result is wrong, we will not run it.

 We have finished explaining the above questions about one-dimensional arrays. Next, we will start explaining the questions about two-dimensional arrays. Do you still remember the knowledge of two-dimensional arrays? The rows and columns of a two-dimensional array start from 0. Take int a[3][4] as an example. The type of this two-dimensional array is int [3][4]. The storage of the two-dimensional array in memory is Consecutive, two-dimensional array rows can be omitted, but columns cannot be omitted! Two-dimensional arrays can be understood as arrays of one-dimensional arrays! The writing method of one-dimensional array is int a[3], that is, the array name + [ ], and the writing method of two-dimensional array is int a[3][4]. Int a[3] can be regarded as the array name, and it can also be written as the array name +[ ]! Treat each two-dimensional row as a one-dimensional element

Next, continue to look at the code!

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]));
}

 int a[3][4] is a two-dimensional array with three rows and four columns, each element is int type, and there are 12 elements in total

Start analyzing! First come first code! 

 printf("%d\n", sizeof(a));

sizeof (array name) calculates the size of the entire array. There are 12 elements in total. The size of each element is 4 bytes, so the size of the entire array is 48 bytes.

 printf("%d\n", sizeof(a[0][0]));

a[0] is a row of a two-dimensional array, a[0][0] is the first element of the first row, sizeof(a[0][0])-->sizeof(int), the calculation result is 4

 printf("%d\n", sizeof(a[0]));

a[0] is the first row of the two-dimensional array, sizeof(a[0]) calculates the size of the entire first row array, and one row has 4 elements, so the size of the entire first row is 16 bytes

 printf("%d\n", sizeof(a[0] + 1));

sizeof(a[0]+1), since it is not placed inside sizeof alone, a[0] represents the address of the first element of the array. a[0]+1 represents the address of the second element of the first row of the two-dimensional array, sizeof(a[0]+1) calculates the size of the address, and the size of the address is 4/8 bytes

 printf("%d\n", sizeof(*(a[0] + 1)));

We analyzed above that a[0]+1 represents the address of the second element in the first row of the two-dimensional array, so *(a[0]+1) represents the second element in the first row of a two-dimensional array element, sizeof calculates the size of the element, and the result is 4 bytes

printf("%d\n", sizeof(a + 1));

Since the array name is not placed inside sizeof alone, the array name represents the address of the first element of the array at this time, a+1 represents the address of the second element of the array, that is, the address of the second row of the two-dimensional array, and sizeof calculates the address size, which results in 4/8

  printf("%d\n", sizeof(*(a + 1)));

 From the above, it can be obtained that a+1 represents the address of the second element of the array, that is, the address of the second row of the two-dimensional array. Dereference it to get the size of the entire array of the second row, and the result is 16

printf("%d\n", sizeof(&a[0] + 1));

a[0] indicates the first row of the two-dimensional array, &a[0] takes out the entire address of the first row of the two-dimensional array, &a[0]+1 gets the address of the second row of the two-dimensional array. sizeof still calculates the size of the address at this time, and the result is 4/8

printf("%d\n", sizeof(*(&a[0] + 1)));

&a[0] takes out the entire address of the first row of the two-dimensional array, &a[0]+1 gets the entire address of the second row of the two-dimensional array, dereferences it and calculates its size, the result is 16 bytes

 printf("%d\n", sizeof(*a));

a is not placed inside sizeof alone, so at this time a represents the address of the first element of the array, the address of the first element of the two-dimensional array a[0], dereferencing it to get the size of the entire first row of the array, and sizeof calculates it , and the result is 16 bytes

 The last question

printf("%d\n", sizeof(a[3]));

 Oh, a[3]? This two-dimensional array only has 3 rows. Will the calculation of the size of a[3] be accessed out of bounds? No, saucers here can rest assured to calculate! Generally speaking, the code needs to be compiled + run to form an executable file. sizeof calculates the result at the compilation stage, so it will not cause out-of-bounds access! sizeof(a[3]) calculates the size of the entire array, and the result is 16 bytes in size

Run the code to see the result

 3. Pointer part

 On the code!

int main()
{
    int a[5] = { 1, 2, 3, 4, 5 };
    int* ptr = (int*)(&a + 1);
    printf("%d,%d", *(a + 1), *(ptr - 1));
    return 0;
}
//程序的结果是什么?

 Array a is a one-dimensional array that stores five elements, and &a takes out the address of the entire array. At this time, the array pointer should be used to receive it, but here it is forced to be converted to int* type.

 a represents the address of the first element of the array, a+1 represents the address of the second element of the array, *(a+1) represents the second element 2 of the array, int*ptr=(int*)(&a+1), ptr The pointing is consistent with the pointing of &a+1, ptr-1 points to the address of a[4] at this time, dereferencing it will get a[4]-->5, so the answer to this question is 2,5. Run the code to see

 


That's all for today's study, let's continue tomorrow!

Guess you like

Origin blog.csdn.net/2301_77886098/article/details/131863664