当前位置:网站首页>C language: deep understanding of pointers and arrays

C language: deep understanding of pointers and arrays

2022-06-13 09:12:00 Caixinzhi

Catalog

One 、sizeof Size judgment and strlen The judgment of length

  Two 、 Specific topic judgment


 

This article will be in the form of a title , Help understand pointers and arrays ( It's a little bit too much ).

One 、sizeof Size judgment and strlen The judgment of length

Knowledge point :

* The array name is the address of the first element of the array , But there are two exceptions :
1.sizeof( Array name ), The array name here represents the whole array , It calculates the size of the entire array , Unit is byte .
2.& Array name , The array name here also represents the entire array , What we get is the address of the array .
    In addition to the above two special cases , All array names are the address of the first element of the array .

Be careful : The following codes are in 32 Run on a bit platform (x86), Address size is 4 Bytes ; If in 64 Under the platform (x64), The address size will change to 8 Bytes .

1. One dimensional array

	int a[] = { 1,2,3,4 };
	printf("%d\n", sizeof(a));        //16
	printf("%d\n", sizeof(a + 0));    //4
	printf("%d\n", sizeof(*a));       //4
	printf("%d\n", sizeof(a + 1));    //4
	printf("%d\n", sizeof(a[1]));     //4
	printf("%d\n", sizeof(&a));       //4
	printf("%d\n", sizeof(*&a));      //16
	printf("%d\n", sizeof(&a + 1));   //4
	printf("%d\n", sizeof(&a[0]));    //4
	printf("%d\n", sizeof(&a[0] + 1));//4 

analysis ( See the picture ):

The case of one-dimensional array is relatively simple , Just grab the first rule , Basically no mistakes .

2. A character array

Add : About strlen Knowledge points of :

sizeof( The operator / keyword ): Don't pay attention to type , Just focus on the size of the occupied space .
strlen( Library function ): For string types only , Focus on... In the string \0 The location of , The calculation is \0 How many characters have appeared before .
Be careful :strlen What you need is an address , Start at this address and look back for characters . The return value of this function is size_t, It's unsigned .

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

	printf("%d\n", strlen(arr));         // Random value 
	printf("%d\n", strlen(arr + 0));     // Random value 
	printf("%d\n", strlen(*arr));        // Report errors 
	printf("%d\n", strlen(arr[1]));      // Report errors 
	printf("%d\n", strlen(&arr));        // Random value 
	printf("%d\n", strlen(&arr + 1));    // Random value 
	printf("%d\n", strlen(&arr[0] + 1)); // Random value 

  analysis ( See the picture ):

 

	char brr[] = "abcdef";
	printf("%d\n", sizeof(brr));         //7
	printf("%d\n", sizeof(brr + 0));     //4
	printf("%d\n", sizeof(*brr));        //1
	printf("%d\n", sizeof(brr[1]));      //1
	printf("%d\n", sizeof(&brr));        //4
	printf("%d\n", sizeof(&brr + 1));    //4
	printf("%d\n", sizeof(&brr[0] + 1)); //4

	printf("%d\n", strlen(brr));         //6
	printf("%d\n", strlen(brr + 0));     //6
	printf("%d\n", strlen(*brr));        // Report errors 
	printf("%d\n", strlen(brr[1]));      // Report errors 
	printf("%d\n", strlen(&brr));        //6
	printf("%d\n", strlen(&brr + 1));    // Random value 
	printf("%d\n", strlen(&brr[0] + 1)); //5

  analysis ( See the picture ):

The string stored in the array is compared with the character stored in the array above , At the end of the string, there will be '\0', So when calculating the length of the array , Can accurately calculate the length , If you want to not return random values in a character array , You need to manually add... To the end of the array '\0'.

	char* p = "abcdef";
	printf("%d\n", sizeof(p));        //4
	printf("%d\n", sizeof(p + 1));    //4
	printf("%d\n", sizeof(*p));       //1
	printf("%d\n", sizeof(p[0]));     //1
	printf("%d\n", sizeof(&p));       //4
	printf("%d\n", sizeof(&p + 1));   //4
	printf("%d\n", sizeof(&p[0] + 1));//4

	printf("%d\n", strlen(p));        //6
	printf("%d\n", strlen(p + 1));    //5
	printf("%d\n", strlen(*p));       // Report errors 
	printf("%d\n", strlen(p[0]));     // Report errors 
	printf("%d\n", strlen(&p));       // Random value 
	printf("%d\n", strlen(&p + 1));   // Random value 
	printf("%d\n", strlen(&p[0] + 1));//5

analysis ( See the picture ):

Character arrays are generally not difficult , If you are careful, you will not make a wrong judgment .

3. Two dimensional array

	int a[3][4] = { 0 };
	printf("%d\n", sizeof(a));           //48
	printf("%d\n", sizeof(a[0][0]));     //4
	printf("%d\n", sizeof(a[0]));        //16
	printf("%d\n", sizeof(a[0] + 1));    //4
	printf("%d\n", sizeof(*(a[0] + 1))); //4
	printf("%d\n", sizeof(a + 1));       //4
	printf("%d\n", sizeof(*(a + 1)));    //16
	printf("%d\n", sizeof(&a[0] + 1));   //4
	printf("%d\n", sizeof(*(&a[0] + 1)));//16
	printf("%d\n", sizeof(*a));          //16
	printf("%d\n", sizeof(a[3]));        //16

analysis ( See the picture ):

 

  Two dimensional arrays are a little harder to judge than one-dimensional arrays , But just remember : A two-dimensional array dereference is the address of a row , Dereference a row of elements again

  Two 、 Specific topic judgment

subject 1:

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

  analysis ( See the picture ):

  subject 2:

#include <stdio.h>

struct Test
{
	int Num;
	char* pcName;
	char cha[2];
	short sBa[4];
}*p;

// hypothesis p The value of is 0x100000
int main()
{
	printf("%p\n", p + 0x1);               //0x100014
	printf("%p\n", (unsigned long)p + 0x1);//0x100001
	printf("%p\n", (unsigned int*)p + 0x1);//0x100004
	return 0;
}

analysis ( See the picture ):

This problem needs to calculate the size of the structure , The following article will talk about the structure memory alignment rules used to calculate the size of the structure , This is to explain the pointer, so we don't describe the structure too much .

subject 3:

#include <stdio.h>
int main()
{
	int a[4] = { 1,2,3,4 };
	int* ptr1 = (int*)(&a + 1);
	int* ptr2 = (int*)((int)a + 1);
	printf("%x,%x", ptr1[-1], *ptr2);   //4,2000000
	return 0;
}

analysis ( See the picture ):

subject 4:

#include <stdio.h>
int main()
{
	int a[3][2] = { (0,1),(2,3),(4,5) };
	int* p;
	p = a[0];
	printf("%d", p[0]);   //1
	return 0;
}

analysis ( See the picture ):

What we should pay attention to here is , There are only... In the initialization array 3 Number , Because it's using () The enclosed ,() Is equivalent to a comma expression , The value is the value of the last expression . If you want to express it in the form of initializing three rows and two columns , Should put the () Switch to { }.

subject 5:

#include <stdio.h>
int main()
{
	int a[5][5];
	int(*p)[4];
	p = a;
	printf("%p,%d\n", &p[4][2] - &a[4][2], &p[4][2] - &a[4][2]);   //FFFFFFFC,-4
	return 0;
}

analysis ( See the picture ):

Be careful : Need to know %p What's printed .

subject 6:

#include <stdio.h>
int main()
{
	int aa[2][5] = { 1,2,3,4,5,6,7,8,9,10 };
	int* ptr1 = (int*)(&aa + 1);
	int* ptr2 = (int*)(*(aa + 1));
	printf("%d,%d", *(ptr1 - 1), *(ptr2 - 1));   //10,5
	return 0;
}

analysis ( See the picture ):

This problem is about two-dimensional arrays , Can be compared with the previous topic 3 One dimensional array of , Their same questions .

subject 7:

#include <stdio.h>
int main()
{
	char* a[] = { "work","at","alibaba" };
	char** pa = a;
	pa++;
	printf("%s\n", *pa);   //at
	return 0;
}

analysis ( See the picture ):

subject 8:

#include <stdio.h>
int main()
{
	char* c[] = { "ENTER","NEW","POINI","FIRST" };
	char** cp[] = { c + 3,c + 2,c + 1,c };
	char*** cpp = cp;
	printf("%s\n", **++cpp);          //POINI
	printf("%s\n", *-- * ++cpp + 3);  //ER
	printf("%s\n", *cpp[-2] + 3);     //ST
	printf("%s\n", cpp[-1][-1] + 1);  //EW
	return 0;
}

analysis ( See the picture ):

This question is a complicated one , But drawing can still be well understood and done .

These are some of the problems of pointers and arrays , Understand these topics , Basically, you can make good use of pointers and arrays . Again : Pointers and arrays for C Language is very important , Well understood and applied .

原网站

版权声明
本文为[Caixinzhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270533005156.html