当前位置:网站首页>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
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 .
边栏推荐
- QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
- pytorch统计模型的参数个数
- An error CV2 is reported when the picture is converted to grayscale cvtColor(img, cv2.COLOR_BGR2GRAY)
- 20211108 is transpose multiply a a a positive definite matrix? What are the necessary and sufficient conditions for a to be a positive definite matrix?
- Opencv gaussianblur() explanation (Sigma value)
- 20211028 Stabilizability
- Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
- Redirect vulnerability analysis of network security vulnerability analysis
- Final principle
- 20211115 矩阵对角化的充要条件;满秩矩阵不一定有n个线性无关的特征向量;对称矩阵一定可以对角化
猜你喜欢
Collection of garbled code problems in idea development environment
Detailed explanation of C language callback function
Online debugging tool Arthas advanced
Drill down to protobuf - Introduction
20211104 why are the traces of similar matrices the same
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
[network security penetration] if you don't understand CSRF? This article gives you a thorough grasp
「解读」华为云桌面说“流畅”的时候,究竟在说什么?
20211104 为什么相似矩阵的迹相同
C/S模型与P2P模型
随机推荐
Spectre record
JUC原子引用与ABA问题
消息中间件
Figure introduction to database neo4j
Map 23 summary
202012 CCF test questions
Mttr/mttf/mtbf diagram
[QNX hypervisor 2.2 user manual] 4.5 building a guest
20211104 why are the traces of similar matrices the same
20211115 任意n阶方阵均与三角矩阵(上三角或者下三角)相似
JUC 原子累加器
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
【安全】零基础如何从0到1逆袭成为安全工程师
Simulink variant model and variant subsystem usage
20211108 A转置乘A是正定矩阵吗?A转置乘A是正定矩阵的充分必要条件是什么?
「解读」华为云桌面说“流畅”的时候,究竟在说什么?
Tutorial (5.0) 03 Security policy * fortiedr * Fortinet network security expert NSE 5
Online debugging tool Arthas advanced
Visual studio tools using shortcut keys (continuous update)
Lecture par lots de tous les fichiers vocaux sous le dossier