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


边栏推荐
- Configure and use Anaconda environment in pycharm
- Together, Shangshui Shuo series] day 9
- Altaro requirements for starting from backup on Hyper-V
- College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
- Capacity expansion mechanism of map
- Differences among bio, NiO and AIO
- Why is go language particularly popular in China
- Calculation method of AUC
- Disassembly and installation of Lenovo r7000 graphics card
- Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
猜你喜欢

穀歌 | 蛋白序列的深度嵌入和比對

XML Configuration File
![[together Shangshui Shuo series] day 7 content +day8](/img/fc/74b12addde3a4d3480e98f8578a969.png)
[together Shangshui Shuo series] day 7 content +day8

Today, many CTOs were killed because they didn't achieve business

谷歌 | 蛋白序列的深度嵌入和比对

Principles of BTC cryptography
![[practical project] autonomous web server](/img/99/892e600b7203c63bad02adb683c8f2.png)
[practical project] autonomous web server

大学校园IP网络广播-厂家基于校园局域网的大学校园IP广播方案设计指南
![Together, Shangshui Shuo series] day 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Together, Shangshui Shuo series] day 9

Win10 install pytullet and test
随机推荐
Today, many CTOs were killed because they didn't achieve business
Altaro VM backup getting started
@Solutions to null pointer error caused by Autowired
Why should we rewrite hashcode when we rewrite the equals method?
Go practice -- design patterns in golang's singleton
[untitled]
Ansible firewall firewalld setting
穀歌 | 蛋白序列的深度嵌入和比對
Go practice -- use redis in golang (redis and go redis / redis)
Final review (Day5)
Transferring images using flask
2022.6.30DAY591
Classification and discussion of plane grab detection methods based on learning
Interview question -- output the same characters in two character arrays
Final review (Day2)
redis 无法远程连接问题。
redis 遇到 NOAUTH Authentication required
"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
Redis 入門和數據類型講解
Principles of BTC cryptography