C语言指针使用的注意点

C语言的指针一向使人头疼,下面是从网上收集的一下关于指针的练习题,忘了作者是谁了,先贴上供大家参考:

第一题:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

main()

{

char *p,*q;

char str[]="Hello,World\n";

q = p = str;

p++;

printf(q);

printf(p);

}

运行结果是什么?____________

第二题:

void fun(char* str1, char* str2)

{

static char buffer[21];

strncpy(buffer, str1, 10);

strncat(buffer, str2, 10);

*str1 = *str2;

str1 = buffer;

}

main()

{

char *str1="ABC\n";

char *str2="BCD\n";

fun(str1, str2);

printf(str1);

printf(str2);

}

程序运行结果是__________________

第三题:

main()

{

short ar[11]={1,2,3,4,5,6,7,8,9,0,11};

short* par=&ar[1];

int i;

for(i=0; i<10; i++)

{

printf("%-5hd%-5hd%-5hd", ar[i], par[i],*(ar+i));

}

}

程序运行结果是__________________

第四题:

main()

{

short *p, *q;

short ar[10]={0};

p = q = ar;

p++;

printf("%5d", p-q);

printf("%5d", (char*)p - (char*)q);

printf("%5d", sizeof(ar)/sizeof(*ar));

}

假设sizeof(short)==2

程序运行结果是__________________

第五题:

int sub(int a, int b)

{

return a-b;

}

main()

{

typedef int (*SUB)(int, int);

SUB psub=sub;

/* psub++; */

printf("%d", psub(10,(10,5)));

}

程序运行结果是__________________, 如果将中间注释掉的语句加上, 编译为什么会报错?

第六题:

main()

{

char* pstrar[3];

int i;

for(i=1; i<3; i++)

{

pstrar[i]=" ";

}

strcpy(pstrar[1], "你好");

/* strcpy(pstrar[0], "世界"); */

printf(pstrar[2]);

}

假设编译器设置字符串常量为可读写,则程序结果是_____________

为什么说注释掉的程序语句是不正确的?

第七题:

main()

{

char *p1,*p2;

{

char* pchar;

char charar[] = "你好,世界";

pchar = "Hello,World!";

p1 = pchar;

p2 = charar;

}

printf(p1);

printf(p2);

}

说出此程序的错误之处?

第八题:

main()

{

int i;

char **p;

int msg[16]={0x40, 0x41, -1, 0x00, 0x01, -1, 0x12, -1, 0x20, 0x27, 0x41, 0x35, -1, 0x51, 0x12, 0x04};

char* strar[]={"bejing", "shanghai", "guangzhou", "guangdong", "Tokyo", "American"};

char* (*pstrar)[6];

pstrar = &strar;

p = strar;

for(i=0; i<16; i++)

{

if(msg[i] == -1)

{

putchar(' ');

continue;

}

else if(msg[i]&0xF0 == 0x40)

{

putchar(p[msg[i]>>4][msg[i]&0x<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="F">0F</chmetcnv>]);

continue;

}

else if(msg[i]&0xF0 == 0x30)

{

putchar(*(strar[msg[i]>>4]+(msg[i]&0x<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="F">0F</chmetcnv>)));

continue;

}

else

{

putchar(*((*pstrar)[msg[i]>>4]+(msg[i]&0x<chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="0" unitname="F">0F</chmetcnv>)));

}

}

}

此题有故弄玄虚之处,但如理解指针,不难解出.

请问此题的运行结果是____________________

第九题:

main()

{

typedef char CA3[2][2][2];

typedef CA3 *PCA3;

typedef char CA2[2][2];

typedef CA2* PCA2[2];

CA3 ca3={'A', '\0', 'B', '\0', 'C', '\0', 'D', '\0'};

PCA3 pca3 = &ca3;

PCA2 pca2={ca3, ca3+1};

int i=0,j=0;

for(i=0; i<2; i++)

{

printf("\n");

printf("%s\n", (char*)pca3[0][i]);

printf("%s\n", (*(pca2+i))[0][1]);

for(j=0; j<2; j++)

{

putchar(*(*(ca3+i)+j)[0]);

putchar(' ');

}

}

}

求输出的结果是:_____________________

第十题:

/*C++ */

#include

class Display

{

public:

virtual int ShowIt(int num) {printf("%d\n", num); return 0;}

int ShowIt(double num) {printf("%lf\n", num); return 0;}

};

class DisplayEx: public Display

{

public:

int ShowIt(int num) {printf("It is Integer, value is %d\n", num); return 0;}

int ShowIt(const char* str) {printf("%s\n", str); return 0;}

};

int main()

{

DisplayEx dpex;

Display *p_base=&dpex;

p_base->ShowIt(168);

p_base->ShowIt(1.68);

dpex.ShowIt("Hello,World\n");

dpex.ShowIt(1.69);

dpex.Display::ShowIt(1.69);

return 0;

}

请说出其运行结果___________________

详细叙述c++编译器实现这一过程的方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值