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


边栏推荐
- 2022.7.2 模拟赛
- 在PyCharm中配置使用Anaconda环境
- 期末复习(Day2)
- Redis expiration elimination mechanism
- Deep embedding and alignment of Google | protein sequences
- Final review (Day6)
- Xaml gradient issue in uwp for some devices
- Robot capture experiment demonstration video
- "C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
- Final review (day3)
猜你喜欢

Simpleitk learning notes

Altaro virtual machine replication failed: "unsupported file type vmgs"
![Ensemble, série shuishu] jour 9](/img/39/c1ba1bac82b0ed110f36423263ffd0.png)
Ensemble, série shuishu] jour 9

(perfect solution) how to set the position of Matplotlib legend freely

Intégration profonde et alignement des séquences de protéines Google

Common interview questions of microservice

How to set up altaro offsite server for replication

配置xml文件的dtd

Brief introduction of realsense d435i imaging principle

Apache+PHP+MySQL环境搭建超详细!!!
随机推荐
Go practice -- gorilla / websocket used by gorilla web Toolkit
Redis encountered noauth authentication required
Why is go language particularly popular in China
Altaro requirements for starting from backup on Hyper-V
配置xml文件的dtd
Go practice -- closures in golang (anonymous functions, closures)
Interview question -- output the same characters in two character arrays
Azure file synchronization of altaro: the end of traditional file servers?
Disassembly and installation of Lenovo r7000 graphics card
Final review (day3)
How to use source insight
redis 遇到 NOAUTH Authentication required
Gan network thought
Final review (Day2)
Why should we rewrite hashcode when we rewrite the equals method?
今天很多 CTO 都是被干掉的,因为他没有成就业务
The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
Covering Safari and edge, almost all mainstream browsers have realized webgl 2.0 support
"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
Configure and use Anaconda environment in pycharm