当前位置:网站首页>Advanced practice of C language (high level) pointer
Advanced practice of C language (high level) pointer
2022-07-07 07:20:00 【ℳ white ℳ night ℳ】
Written examination questions for advanced pointer
Introduction
Here I will share with you all the written examination questions I have seen , And explain .
This chapter uses 32 Bit platform .
The test part
One
#include <stdio.h>
int main()
{
char str1[] = "hello baiye.";
char str2[] = "hello baiye.";
const char* str3 = "hello baiye.";
const char* str4 = "hello baiye.";
if (str1 == str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if (str3 == str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}
The result of the code run is :
here str3 and str4 Points to the same constant string .C/C++ The constant string is stored in a separate memory area , When a few pointers . When pointing to the same string , They actually point to the same block of memory . However, when initializing different arrays with the same constant string, different memory blocks will be opened up . therefore str1 and str2 Different ,str3 and str4 Different .
Two
#include <stdio.h>
int main()
{
// One dimensional array
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));// The array list is placed alone in sizeof Is to calculate the length of the entire array
printf("%d\n", sizeof(a + 0));// Array names are not placed separately sizeof Inside , It is equal to the address of the first element , Add 0 Equal to not moving , It is the length of the address of the first element (64 One platform is 8 Bytes )
printf("%d\n", sizeof(*a));// The same is not put inside alone , After dereferencing, the length of the first element is calculated
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(a[1]));
printf("%d\n", sizeof(&a));// It's still the length of an address , As long as it's the address , No 4 Namely 8
printf("%d\n", sizeof(*&a));// This is equal to taking out the address of the array , Then dereference equals an array in sizeof Internal
printf("%d\n", sizeof(&a + 1));// This is to skip an array , But it is also the length of the address that needs to be calculated
printf("%d\n", sizeof(&a[0]));// What is taken out is the array subscript 0 Address of element
printf("%d\n", sizeof(&a[0] + 1));
return 0;
}
Code run results :
3、 ... and
#include <stdio.h>
#include <string.h>
int main()
{
// A character array
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));//char The length of the type is one byte
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));//stelen Is to pass the first address of the string you want to calculate , Because I read \0 Will stop , Here we put it in character by character , So it's a random value
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));// The address of the first element of the string is not passed in , There must be no result
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
Code run results :
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));//sizeof Will put the end \0 Add in
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));
}
Code run results :
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)); This is the first address of the string , It is also the address of the first character
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));
Code run results :
#include <stdio.h>
#include <string.h>
int main()
{
// Two dimensional array
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));// The size of the first element of a two-dimensional array
printf("%d\n", sizeof(a[0]));// The first element of a two-dimensional array , Equal to the array of the first row , Equal to a one-dimensional array ( Array name ), Then the length of the one-dimensional array
printf("%d\n", sizeof(a[0] + 1));// The array name of a one-dimensional array is not placed separately sizeof Inside , So it is the length of the address of the second element of a one-dimensional array
printf("%d\n", sizeof(*(a[0] + 1)));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(*(a + 1)));// Although this is to skip a two-dimensional array before dereferencing , however sizeof I won't see what elements are in it , So it's ok if the array goes out of bounds , Because it already knows that there is 4 The shaping element of
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]));
return 0;
}
Code run results :
Four
1.
#include <stdio.h>
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 ?
// It is known that , Structure Test The variable size of type is 20 Bytes
int main()
{
printf("%p\n", p + 0x1);// Here is the pointer size of the structure type 20, But because it needs to be converted to 16 Base, so it's plus 14
printf("%p\n", (unsigned long)p + 0x1);// Cast the address cast type to decimal and add 1 Then it is converted to hexadecimal ( This is the addition of shaping )
printf("%p\n", (unsigned int*)p + 0x1);// Here is the conversion to an integer pointer , Add 1 Equal to skipping the length of an integer is equal to adding 4
return 0;
}
Code results :
2.
#include <stdio.h>
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;
}
You must be right ptr2 Very confused ;
Because the array name ( First element address ) Cast to int, That is to add one after becoming an integer , It means that our original address skipped a byte .
My computer is a small end storage method
Because it is stored upside down ( Put high order to high address , Put the low order to the low address ), Then we should also 00 00 00 02 Take it out upside down , So there is the above result .
3.
#include <stdio.h>
int main()
{
int a[3][2] = {
(0, 1), (2, 3), (4, 5) };// Be careful , Inside are parentheses
int* p;
p = a[0];
printf("%d", p[0]);
return 0;
}
Running results :
4.
#include <stdio.h>
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;
}
The operation results are as follows :
Pointer subtraction is the number of intermediate elements .
p[4][2] Can be seen as (*(*p+4)+2)
The second result is that the low address decreases the high address , So it is -4
The first result is because of %p Print , So the -4 The complement of is converted into 16 Base number , It becomes FFFFFFC
5.
#include <stdio.h>
int main()
{
char* a[] = {
"work","at","alibaba" };
char** pa = a;// Yes a The address of
pa++;
printf("%s\n", *pa);
return 0;
}
Running results :
6.
#include <stdio.h>
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;
}
Running results :
This problem looks like this at the beginning
charc What is stored is the initial address of each string ,char**cp Pointing to charc The address of ,char*cpp Pointing to charcp The address of .
first , because ++cpp So I moved here
After two dereferences, we get POINT.
The second one is ++cpp And then I'll explain the quotation , Then you have to subtract and then de quote , Finally, add three
We found that ,cp The third element in is from c+1 Turned into c, the reason being that - - Operators and ++ It is also permanent , Unlike c+1-1 Not assigned to yourself . As for why the red arrow points E, It's because the following plus three moves the initial letter pointing to this string backward by three characters .
The third one is looking for cp Medium -2 Location , Because the first two ++ Led us to find c+3 That position ( The blue arrow indicates )
Be careful , This time cpp It's just a temporary move , find cpp[-2] The position of will return to its original position . After dereferencing, another one is added 3 Same as above , What's printed is ST.
Finally, the fourth one is easy to understand , First find cpp[-1] The location of , And then we found it c+2,c+2 The point is POINT This string , Because I have to find cp[-1] So here we are NEW The location of , And because of the last +1 So it became EW.
summary
1. The meaning of array names :
sizeof( Array name ), The array name here represents the entire array , It calculates the size of the entire array .
& Array name , The array name here represents the entire array , It takes out the address of the entire array .
In addition, all array names represent the address of the first element .
2.cpp[-2][1] Such code can be converted into (*(*cpp-2)+1)
边栏推荐
- FPGA course: application scenario of jesd204b (dry goods sharing)
- JS decorator @decorator learning notes
- Implementation of AVL tree
- 2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
- ViewModelProvider. Of obsolete solution
- ROS2规划系统plansys2简单的例子
- Apache AB stress test
- MySQL service is missing from computer service
- 组件的嵌套和拆分
- A slow SQL drags the whole system down
猜你喜欢
随机推荐
Mobx knowledge point collection case (quick start)
[explanation of JDBC and internal classes]
弹性布局(一)
【JDBC以及内部类的讲解】
多线程与高并发(9)——AQS其他同步组件(Semaphore、ReentrantReadWriteLock、Exchanger)
Several important steps to light up the display
Apache AB stress test
The currently released SKU (sales specification) information contains words that are suspected to have nothing to do with baby
身边35岁程序员如何建立起技术护城河?
Please tell me how to monitor multiple schemas and tables by listening to PgSQL
Communication of components
MySQL view bin log and recover data
Kuboard无法发送邮件和钉钉告警问题解决
Nesting and splitting of components
Tumor immunotherapy research prosci Lag3 antibody solution
Pass child component to parent component
readonly 只读
机器人技术创新与实践旧版本大纲
修改Jupyter Notebook文件路径
Sqlserver multithreaded query problem