地址连接:http://topic.csdn.net/u/20110729/12/9973E5A4-A414-4714-871F-905A85612297.html
1,The output for this program is: (a) 3 (b) 5 (c) 0
#include<setjmp.h>
static jmp_buf buf;
int main() {
volatileint b;
b =3;
if(setjmp(buf)!=0) {
printf("%d", b);
exit(0);
}
b=5;
longjmp(buf , 1);
return 0;
}
分析:主要考察setjump和longjump这两个函数的作用。从英文字面意思我们可以知道是跳转,程序的非本地跳转。
关于这两个函数的详细介绍可以参考维基百科:http://zh.wikipedia.org/wiki/Setjmp.h#cite_note-macro-0
只要知道了这两个函数的功能,就很容易知道本题的答案为:(b) 5
2,The output for this program is: (a) 3 (b) 5 (c) 6 (d) 7
struct node { int a; int b;int c; };
int main() {
struct node s= {3, 5,6 };
struct node*pt = &s;
printf("%d" ,*(int*)pt);
return 0;
}
分析:程序的意思是将一个结构体指针转换为整型指针,结构体是由三个整型的变量构成的,存放在内存的位置是连续的。此结构体的大小为12字节,int型的为4字节,我们知道在32位机器上面,指针类型占用4个字节。将结构体类型的指针转换为int型指针,那么int型指针就指向了结构体中第一个整型变量。所以输出结果为:(a) 3
3,What function of x and n is compute by this code segment?
(a) x^n (b) x*n (c) n^x (d) None of the above
int foo ( int x , int n) {
int val;
val =1;
if (n>0) {
if (n%2== 1) val= val *x;
}
return val;
}
int main() {
int a[ 5 ] = { 1 , 2 , 3 , 4 , 5 };
int * ptr = ( int * )( & a + 1 );
printf( " %d %d " , * (a + 1 ), * (ptr - 1 ) );
return 0 ;
}
int a [ 3 ][ 3 ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 },{ 7 , 8 , 9 }};
foo(a);
printf( " %d " , a[ 2 ][ 1 ]);
return 0 ;
}
void foo( int b[][ 3 ]) {
++ b;
b[ 1 ][ 1 ] = 9 ;
}
int main() {
int a, b,c, d;
a = 3 ;
b = 5 ;
c = a,b;
d = (a,b);
printf( " c=%d " ,c);
printf( " d=%d " ,d);
return 0 ;
}
7,The output for this program is:(a) 2 3 5 6 (b) 2 3 4 5 (c) 4 5 0 0 (d) None of the above
int main() {
int a[][ 3 ] = { 1 , 2 , 3 , 4 , 5 , 6 };
int ( * ptr)[ 3 ] = a;
printf( " %d %d " ,( * ptr)[ 1 ], ( * ptr)[ 2 ] );
++ ptr;
printf( " %d %d " ,( * ptr)[ 1 ], ( * ptr)[ 2 ] );
return 0 ;
}
int *p[2] ={&m,&n}; //指针数组,数组中有两个整型指针
printf("%d %d",*p[0],*p[1]); //输出m和n的值
int (*ptr)[3]=a; //定义一个数组指针,指向二维数组a,ptr也就是数组a的首地址
printf("%d %d" ,(*ptr)[1], (*ptr)[2] ); //输出 2 3
++ptr; //指向下一个相当于 ptr = a+3,指向a[1]
printf("%d %d" ,(*ptr)[1], (*ptr)[2] ); //输出 5 6
8,Which of the above three functions are likely to cause problem with pointers
(a) Only f3 (b) Only f1 and f3 (c) Only f1 and f2 (d) f1 , f2 ,f3
int * f1( void ) {
int x = 10 ;
return ( & x);
}
int * f2( void ) {
int * ptr;
return ptr;
}
int * f3( void ) {
int * ptr;
ptr = ( int * ) malloc( sizeof ( int ));
return ptr;
}