地址連接: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;
}