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


边栏推荐
- Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
- 配置xml文件的dtd
- Ensemble, série shuishu] jour 9
- 2022.DAY592
- Go practice -- gorilla / websocket used by gorilla web Toolkit
- Redis使用Lua脚本简介
- [untitled]
- AtCoder Beginner Contest 258(A-D)
- "C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
- Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
猜你喜欢

Win10 install pytullet and test
![Together, Shangshui Shuo series] day 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Together, Shangshui Shuo series] day 9

求质数的方法

SimpleITK学习笔记

【一起上水硕系列】Day 10

Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires

Training method of grasping angle in grasping detection

3dslam with 16 line lidar and octomap

DEX net 2.0 for crawl detection

"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
随机推荐
期末复习DAY8
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Deploy crawl detection network using tensorrt (I)
Technical analysis of qianyuantong multi card aggregation router
2022.DAY592
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
chromedriver对应版本下载
Get and monitor remote server logs
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
Why should we rewrite hashcode when we rewrite the equals method?
DEX net 2.0 for crawl detection
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Classification and discussion of plane grab detection methods based on learning
2022.7.2 simulation match
牛客网 JS 分隔符
"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
32GB Jetson Orin SOM 不能刷机问题排查
Redis 入门和数据类型讲解
获取并监控远程服务器日志
6.23 warehouse operation on Thursday