当前位置:网站首页>Analysis of 8 classic C language pointer written test questions

Analysis of 8 classic C language pointer written test questions

2022-07-08 00:43:00 real Wangyanbin

Pen test 1:

#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;
}
// What is the result of the program ?

 Insert picture description here

Pen test 2:

#include<stdio.h>
// 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 ?
// 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;
}

 Insert picture description here

Pen test 3

#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;
}
// What is the output 

 Insert picture description here

Pen test 4

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

 Insert picture description here

Pen test 5

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

 Insert picture description here

Pen test 6

#include<stdio.h>
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;
}

 Insert picture description here

Pen test 7

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

 Insert picture description here

Pen test 8:

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

 Insert picture description here

原网站

版权声明
本文为[real Wangyanbin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/189/202207072251133297.html