当前位置:网站首页>Advanced practice of C language (high level) pointer

Advanced practice of C language (high level) pointer

2022-07-07 07:20:00 ℳ white ℳ night ℳ

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 :
 Insert picture description here
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 .
 Insert picture description here

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 :
 Insert picture description here

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 :
 Insert picture description here

#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 :
 Insert picture description here

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 :
 Insert picture description here

#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 :
 Insert picture description here

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 :
 Insert picture description here
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; 
}

 Insert picture description here
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
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
Pointer subtraction is the number of intermediate elements .
 Insert picture description here
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 :
 Insert picture description here
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 :
 Insert picture description here
This problem looks like this at the beginning
 Insert picture description here
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
 Insert picture description 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
 Insert picture description here
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 )
 Insert picture description here

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)

原网站

版权声明
本文为[ℳ white ℳ night ℳ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070330261957.html