C语言面试问答5

2023-06-28,,

12个滑稽的C语言面试问答——《12个有趣的C语言问答》评析(5)

前文链接:http://www.cnblogs.com/pmer/archive/2013/09/17/3327262.html

A,局部变量的返回地址

Q:下面的代码有问题吗?如果有,如何修改?

?

#include<stdio.h>
 
int* inc(int val)
{
  int a = val;
  a++;
  return &a;
}
 
int main(void)
{
    int a = 10;
 
    int *val = inc(a);
 
    printf("\n Incremented value is equal to [%d] \n", *val);
 
    return 0;
}

A:虽然上面的代码有时运行会很好,但是在方法 inc() 中有很严重的隐患。当inc()方法执行后,再次使用局部变量的地址就会造成不可估量的结果。解决之道就是传递变量a的地址给main()。

Answer: Though the above program may run perfectly fine at times but there is a serious loophole in the function ‘inc()’. This function returns the address of a local variable. Since the life time of this local variable is that of the function ‘inc()’ so after inc() is done with its processing, using the address of its local variable can cause undesired results. This can be avoided by passing the address of variable ‘a’ from main() and then inside changes can be made to the value kept at this address.

评:

  这个主要是翻译的问题。对照一下原文就会发现,译文不但漏掉了很多内容没有翻译,最严重的是把“by passing the address of variable ‘a’ from main()”给翻译成了“传递变量a的地址给main()”,彻底颠覆了原文的意思。原意是传递main()中变量a的地址,即调用形式为int(&a),而译文的意思依然是代码中那种错误写法的描述。

B,处理 printf() 参数

Q:以下代码输出请问是什么?

?

#include<stdio.h>
 
int main(void)
{
    int a = 10, b = 20, c = 30;
 
    printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));
 
    return 0;
}

A:输出将是

1 110..40..60

这是因为参数都是从右向左处理的,然后打印出来却是从左向右。

Answer: The output of the above code would be :

110..40..60

This is because the arguments to the function are processed from right to left but are printed from left to right.

评:

  这个问题和解答都很狗血!

  “参数都是从右向左处理的”是无中生有。C标准规定

  The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

  也就是说实参的求值次序没做规定(unspecified),编译器可以自己安排计算次序,无论怎样安排都不违背标准。从右向左或从右到左都可以。断言“参数都是从右向左处理”,是这个解答中第一个错误。

  前面引用条文中还提到了在实际调用前存在一个序点(sequence point),对于代码中

?

printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2))

这个函数调用来说,前一个序点就是之前的“;”。

  C语言的另一个规定是:

  Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.Furthermore, the prior value shall be read only to determine the value to be stored.

  这两句话是什么意思呢?前一句说的是两个相邻序点之间,一个数据对象的值最多可以改变一次。如果改变多次,就是未定义行为。譬如写出下面代码

?

printf("%d,%d,%d,%d\n", ++i, --i, i++, i--);

就是出于典型的对未定义行为的无知。而津津乐道、煞有介事地讨论这种根本没有意义的代码(i++和++i作为参数时的编译器处理方式分析~) ,就如同谭浩强津津乐道地讨论a+=a-=a*a一样荒唐(参见“牙里长嘴”和“a+=a-=a*a” ),我擦,岂不是滑天下之大稽?只有根本没有技术能力又缺乏自知之明的怨妇,才可能无聊到做这种荒谬无价值的事情。

  标准条文中的后面一句有些晦涩,它的意思是数据对象中先前的值只能是用来确定数据对象中后来存储的值,例如:

?

int i = 1;
i = i + 1 ;

  i只改变了一次,且i的原值(1)用来确定i后来存储的值(2)。

  如果数据对象的值在两个序点之间只改变一次,且数据对象的原值不是用来确定数据对象后来的值,例如:

?

int i = 1 , j ;
j = i + (i = 2) ;

则属于未定义行为。因为表达式中第一个i的值不是为了确定i将要存储的值。没人说的清这个i + (i = 2) 这个表达式的值应该是4还是3。

  回过头再看问答代码中的

?

printf("\n %d..%d..%d \n", a+b+c, (b = b*2), (c = c*2));

b和c都被改变一次,但由于在表达式a+b+c中,b和c的值不是用来确定b和c最终的值,因此这段代码属于没有意义的未定义行为。

  因此,这个问答一共错了两处。一处是说“参数都是从右向左处理的”,另一处就是代码行为未定义。

(全文完)

 
 

 
标签: C语言, 面试

C语言面试问答5的相关教程结束。

《C语言面试问答5.doc》

下载本文的Word格式文档,以方便收藏与打印。