(a) 函数的指针,该函数以
两个指向浮点数(float)的指针(pointer)作为参数(arguments) Pointer to function of having
two arguments that is pointer to float (b) 整型 (c) 函数的指针,该函数以
两个指向浮点数(float)的指针(pointer)作为参数(arguments),并且函数的返回值类型是整型 Pointer to
function having two argument that is pointer to float and return int (d)
以上都不是
main() { int num; num = ripple ( 3,
5,7); printf( " %d" , num); }
int ripple (int n,
...) { int i , j; int k; va_list p;
k= 0; j =
1; va_start( p , n);
for (; j { i
= va_arg( p , int); for (; i; i &=i-1 )
++k; } return k; }
这段程序的输出是:
(a) 7 (b) 6 (c)
5 (d) 3
第16题:考查静态变量的知识
int counter (int
i) { static int count =0; count = count +i; return (count
); } main() { int i , j;
for (i=0; i <=5; i++)
j = counter(i); }
本程序执行到最后,j的值是:
(a) 10 (b) 15 (c)
6 (d) 7
详细参考答案
第1题:
(b) volatile字面意思是易于挥发的。这个关键字来描述一个变量时,意味着
给该变量赋值(写入)之后,马上再读取,写入的值与读取的值可能不一样,所以说它"容易挥发"的。 这是因为这个变量可能一个寄存器,直接与外部设备相连,你写入之后,该寄存器也有可能被外部设备的写操作所改变;或者,该变量被一个中断程序,或另一个进程 改变了. volatile
variable isn't affected by the optimization. Its value after the longjump is the
last value variable assumed.
b last value is 5 hence 5 is
printed.
setjmp : Sets up for nonlocal goto /* setjmp.h*/
Stores
context information such as register values so that the lomgjmp function can
return control to the statement following the one calling setjmp.Returns 0 when
it is initially called.
Transfers control to the statement where the call to setjmp
(which initialized buf) was made. Execution continues at this point as if
longjmp cannot return the value 0.A nonvolatile automatic variable might be
changed by a call to longjmp.When you use setjmp and longjmp, the only automatic
variables guaranteed to remain valid are those declared volatile.
Note:
Test program without volatile qualifier (result may very)
第2题:
(a) The members of structures have address in increasing order of their
declaration. If a pointer to a structure is cast to the type of a pointer to its
first member, the result refers to the first member.
第3题: (a) Non
recursive version of the program
int what ( int x ,
int n) { int val; int product; product =1; val
=x;
while(n>0) { if (n%2 == 1) product =
product*val; n = n/2; val = val* val; } }
/* Code
raise a number (x) to a large power (n) using binary doubling strategy
*/ Algorithm description
(while n>0) { if next most
significant binary digit of n( power) is one then multiply accumulated
product by current val , reduce n(power) sequence by a factor of two
using integer division . get next val by multiply current value of itself
}
第4题: (c) type of a is array of
int type of &a is pointer to array of int
(8.82 KB)
2010-1-3 08:12
Taking a pointer to the
element one beyond the end of an array is sure to
work.
第5题: (b)
(11.7 KB)
2010-1-3 08:12
第6题: (c) The comma
separates the elements of a function argument list. The comma is also used as an
operator in comma expressions. Mixing the two uses of comma is legal, but you
must use parentheses to distinguish them. the left operand E1 is evaluated as a
void expression, then E2 is evaluated to give the result and type of the comma
expression. By recursion, the expression
E1, E2, ..., En
results
in the left-to-right evaluation of each Ei, with the value and type of En giving
the result of the whole expression.
第9题: (b) sizeof 操作符给出其操作数需要占用的空间大小,它是在编译时就可确定的,所以其操作数即使是一个表达式,也不需要在运行时进行计算.(
++i + ++ i )是不会执行的,所以 i 的值还是3
第10题: (a) 很显然选a. f1交换*p 和
q的值,f1执行完后, *p 和 q的值的确交换了, 但 q的改变不会影响到 b的改变, *p 实际上就是
a 所以执行f1后, a=b=5 这道题考查的知识范围很广,包括typedef自定义类型,函数指针,指针数组 void(*p[ 2 ]) (
int *,
int); 定义了一个函数指针的数组p,p有两个指针元素. 元素是函数的指针,函数指针指向的函数是一个带2个参数,返回void的函数,所带的两个参数是
指向整型的指针,和整型 p[ 0 ] = f1; p[ 1 ] = f2 contain address of function .function
name without parenthesis represent address of function Value and address of
variable is passed to function only argument that is effected is a (address is
passed). Because of call by value f1, f2 can not effect
b
在C编译器通常提供了一系列处理可变参数的宏,以屏蔽不同的硬件平台造成的差异,增加程序的可移植性。这些宏包括va_start、
va_arg和va_end等。 采用ANSI标准形式时,参数个数可变的函数的原型声明是: type funcname(type para1,
type para2,
...) 这种形式至少需要一个普通的形式参数,后面的省略号不表示省略,而是函数原型的一部分。type是函数返回值和形式参数的类型。
不同的编译器,对这个可变长参数的实现不一样
,gcc4.x中是内置函数.
关于可变长参数,可参阅
程序分析
va_list
p; /*定义一个变量 ,保存 函数参数列表 的指针*/ va_start( p , n); /*用va_start宏 初始化 变量p,
va_start宏的第2个参数n ,
是一个固定的参数, 必须是我们自己定义的变长函数的最后一个入栈的参数
也就是调用的时候参数列表里的第1个参数*/ for (; j{ i = va_arg( p , int); /*va_arg取出当前的参数,
并认为取出的参数是一个整数(int) */ for (; i; i
&=i-1 ) /*判断取出的i是否为0*/ ++k; /*
如果i不为0, k自加, i与i-1进行与逻辑运算, 直到i
为0
这是一个技巧,下面会谈到它的功能*/ }
当我们调用ripple函数时,传递给ripple函数的 参数列表的第一个参数n的值是3
.
va_start 初始化 p士气指向第一个未命名的参数(n是有名字的参数) ,也就是 is 5 (第一个).