当前位置:网站首页>[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
2022-07-03 05:38:00 【Sobtemesa】
Keep going.
Catalog
Pointer written test questions
sizeof And strlen
brief introduction :
sizeof yes C/C++ One of them The operator (operator), Simply put, its function is to return the proportion of an object or type Number of memory bytes .
notes :sizeof It's the operator , It's not a function , You don't need to include any header files when using .
strlen yes c/c++ One of them function , Its function is calculation The length of a specified string .strlen What is done is the work of a counter , It comes from somewhere in memory ( It can start with a string , Somewhere in the middle , Even some uncertain memory area ) Start scanning , Until I met the first character string Terminator '\0' until , Then return the counter value ( Length does not contain '\0').
notes :strlen Is the function , It is necessary to introduce The header file .string.h or cstring.
The function prototype :
size_t strlen(const char *string);
(typedef
unsigned
int
size_t
;)
sizeof And strlen The difference between :
sizeof The empty character \0 Count in , and strlen Will not \0 Count in .
subject :
Keep four tenets in mind before doing questions :
The meaning of array names :
***1.sizeof( Array name ), The array name here represents the whole array , It calculates the size of the entire array .
***2.& Array name , The array here represents the whole array , It takes out the address of the entire array .
3. In addition, all array names represent the address of the first element .
4. A few bytes of a pointer is language independent , It's about the addressing capability of the system .
A pointer in 32 Bit operating system , Occupy 4 Bytes
A pointer in 64 Bit operating system , Occupy 8 Bytes
1. One dimensional array
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));
answer (32 Bit platform ):
analysis :
2. A character array
1)
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));
analysis :
2)
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
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));
answer :
3)
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));
answer :
analysis :
4)
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));
analysis :
3. Two dimensional array
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]));
answer :
analysis :
Pointer written test questions
1)
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);
printf( "%d,%d", *(a + 1), *(ptr - 1));
return 0;
}
2)
// Because I haven't learned the structure yet , The size of the structure is 20 Bytes
struct Test
{
int Num;
char *pcName;
short sDate;
char cha[2];
short sBa[4];
}*p;
// hypothesis p The value of is 0x100000. What are the values of the expressions in the following table ?
int main()
{
printf("%p\n", p + 0x1);
printf("%p\n", (unsigned long)p + 0x1);
printf("%p\n", (unsigned int*)p + 0x1);
return 0;
}
3)
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);
return 0;
}
answer :
analysis :
4)
int main()
{
int a[3][2] = { (0, 1), (2, 3), (4, 5) };
int *p;
p = a[0];
printf( "%d", p[0]);
return 0;
}
answer :
analysis :
5)
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]);
return 0;
}
answer :
6)
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));
return 0;
}
answer :
7)
int main()
{
char *a[] = { "work", "at", "alibaba" };
char**pa = a;
pa++;
printf("%s\n", *pa);
return 0;
}
answer :
analysis :
8)
int main()
{
char *c[] = {"ENTER","NEW","POINT","FIRST"};
char**cp[] = {c+3,c+2,c+1,c};
char***cpp = cp;
printf("%s\n", **++cpp);
printf("%s\n", *--*++cpp+3);
printf("%s\n", *cpp[-2]+3);
printf("%s\n", cpp[-1][-1]+1);
return 0;
}
answer :
analysis :
边栏推荐
- 请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
- 期末复习(day3)
- Altaro virtual machine replication failed: "unsupported file type vmgs"
- Go practice -- closures in golang (anonymous functions, closures)
- "C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
- Troubleshooting of 32GB Jetson Orin SOM failure to brush
- Can altaro back up Microsoft teams?
- Ansible firewall firewalld setting
- Kubernetes resource object introduction and common commands (V) - (configmap)
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
猜你喜欢
2022.DAY592
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Progressive multi grasp detection using grasp path for rgbd images
ES7 easy mistakes in index creation
Mapbox tasting value cloud animation
大二困局(复盘)
Deploy crawl detection network using tensorrt (I)
3dslam with 16 line lidar and octomap
Today, many CTOs were killed because they didn't achieve business
Ensemble, série shuishu] jour 9
随机推荐
Go practice -- use JWT (JSON web token) in golang
Go practice -- gorilla / websocket used by gorilla web Toolkit
Why is go language particularly popular in China
Learn libcef together -- set cookies for your browser
Best practices for setting up altaro VM backups
Altaro set grandfather parent child (GFS) archiving
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Skip table: principle introduction, advantages and disadvantages of skiplist
Principles of BTC cryptography
Transferring images using flask
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
期末复习(Day2)
Calculation method of AUC
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
The IntelliJ platform completely disables the log4j component
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
期末复习(DAY6)
Technical analysis of qianyuantong multi card aggregation router
Training method of grasping angle in grasping detection
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)