当前位置:网站首页>C language pointer interview question - the second bullet

C language pointer interview question - the second bullet

2022-07-04 09:38:00 Hair is not as much as code

Catalog

The first question is

The second question is  

  Third question

  Fourth question

  Fifth question

Sixth question  

  Question seven

  The eighth question


 

The first question is

#include<stdio.h>
int main()
{
	int a[5] = { 1, 2, 3, 4, 5 };
	int* ptr = (int*)(&a + 1); 
	printf("%d,%d", *(a + 1), *(ptr - 1));
	return 0;
}

 

&a The type is int(*)[5], there (int*) It is mandatory to cast &a Type conversion to int*

ptr-1 Then it will point to 5, Then dereference , final result *(ptr-1) be equal to 5

a Point to first element ,a+1 Point to the second element

The second question is  

#include<stdio.h>
struct Test
{
	int Num;
	char* pcName;
	short sDate;
	char cha[2];
	short sBa[4];
}*p=(struct Test*)0x100000;
// 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);
	printf("%p\n", (unsigned long)p + 0x1);
	printf("%p\n", (unsigned int*)p + 0x1);
	return 0;
}

 

because Test The size of the type is 20 Bytes , and p Is precisely test type  ,0x1 yes 16 Number in hexadecimal 1, yes 1*16^0,

p+0x1: It is equivalent to giving p Add twenty bytes , and p The content is 0x1000000, This is a 16 Number in hexadecimal , We should put this 20 Convert to 16 Number in hexadecimal , The result of the conversion is 14, So the answer is 0x100000+14=0x100014

(unsigned long)p+0x1: Is to put p Cast to integer ( Unsigned long shaping ), After plastic surgery, the result is 1048576, Then add 1(16 It's binary 1 and 10 It's binary 1 identical ), Turn into 1048577, To 16 Into the system for 0x100001

(unsigned int*)p: hold p Cast to (unsigned int *) type , The size of this type of permission is four bytes , When p+0x1=p+1 after , Because the permission size is 4 Bytes , therefore p Across 4 Bytes, so the result is 0x100004

  Third question

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; 
}

 

ptr1 The original type of is int(*)[4], Cast type to (int*)ptr[-1] How to get , Please look at the picture ,ptr[-1]=*(ptr1+(-1))=*(ptr1-1)

*ptr2, The end result is because *ptr It is an integer dereference , So the next four bytes are accessed , The final result is related to small end storage

 

  Fourth question

#include <stdio.h>
int main()
{
	int a[3][2] = { (0, 1), (2, 3), (4, 5) };
	int* p;
	p = a[0];
	printf("%d", p[0]);
	return 0;
}

 ​​​​​​​

  Fifth question

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; 
}

 

 

 

Here is the -4 Print it as an address  

Sixth question  

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; 
}

 

 

  Question seven

#include <stdio.h>
int main()
{
 char *a[] = {"work","at","alibaba"};
 char**pa = a;
 pa++;
 printf("%s\n", *pa);
 return 0; 
}

 

 

because pa The object is char * Of , So every time 1, Add one more char * Type size  

  The eighth question

 

#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;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原网站

版权声明
本文为[Hair is not as much as code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040916599402.html