当前位置:网站首页>[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 :


边栏推荐
- Ensemble, série shuishu] jour 9
- Go practice -- closures in golang (anonymous functions, closures)
- Troubleshooting of 32GB Jetson Orin SOM failure to brush
- Interview question -- output the same characters in two character arrays
- 3dslam with 16 line lidar and octomap
- Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn
- Analysis of the example of network subnet division in secondary vocational school
- [together Shangshui Shuo series] day 7 content +day8
- 期末复习(Day2)
- Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
猜你喜欢

Altaro set grandfather parent child (GFS) archiving

PHP笔记超详细!!!

DEX net 2.0 for crawl detection

Go practice -- gorilla / websocket used by gorilla web Toolkit

Common interview questions of microservice

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)

Disassembly and installation of Lenovo r7000 graphics card

小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计

求质数的方法

Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
随机推荐
Deploy crawl detection network using tensorrt (I)
聊聊如何利用p6spy进行sql监控
2022.6.30DAY591
Differences among bio, NiO and AIO
2022.6.30DAY591
Best practices for setting up altaro VM backups
MySQL startup error: several solutions to the server quit without updating PID file
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Capacity expansion mechanism of map
大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
[untitled]
请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
Can altaro back up Microsoft teams?
Skip table: principle introduction, advantages and disadvantages of skiplist
XML Configuration File
Kubernetes resource object introduction and common commands (V) - (configmap)
[Shangshui Shuo series together] day 10
Pessimistic lock and optimistic lock of multithreading
SimpleITK学习笔记
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller