第二十一题

What is the potential problem with the following C program?
#include <stdio.h>
int main()
{
char str[];
printf("Enter the string:");
scanf("%s",str);
printf("You entered:%s\n",str); return ;
}
题目讲解:
易造成数组越界,scanf改成如下形式比较保险
scanf("%79s",str);
不管输入多少个字节,最多只读取79个字节。

第二十二题

What is the output of the following program?
#include <stdio.h>
int main()
{
int i;
i = ;
printf("i : %d\n",i);
printf("sizeof(i++) is: %d\n",sizeof(i++));
printf("i : %d\n",i);
return ;
}
题目讲解:
输出为:
i: 10
sizeof(i++) is:4
i: 10
sizeof(i++),等效于sizeof(int)。sizeof的值在编译时决定,不会执行i++。
sizeof的更多讲解见第一题。

第二十三题

Why does the following program give a warning? (Please remember that sending a normal pointer to a function requiring const pointer does not give any warning)
#include <stdio.h>
void foo(const char **p) { }
int main(int argc, char **argv)
{
foo(argv);
return ;
}
题目讲解
  • 关键字const
用const修饰变量,指定该变量为只读。

const int a = ;//变量a只读

const int *p;//p指向的int型数只读,p可以被修改

int *const p;//p为只读,p指向的int型数可以被修改
  • 带const的一级指针和二级指针赋值
int a = ;

const int *p;

p = &a;//一级const指针赋值时,可以不将右边的指针转换为const型
int *p;

const int **pp;

pp = (const int **)&p;//二级const指针赋值时,必须将右侧二级指针转换为const型
  • 带const的一级指针和二级指针传参
void foo(const char *p) {}
int main()
{
char *p;
foo(p);//p不用转化为const型
return ;
}
void foo(const char **pp) {}
int main()
{
char **pp;
foo((const char **)pp);//pp必须转化为const型
return ;
}

第二十四题

What is the output of the following program?
#include <stdio.h>
int main()
{
int i;
i = ,,;
printf("i:%d\n",i);
return ;
}
题目讲解:
同第十题。
输出: i:1
逗号运算符在所有的运算符中优先级最低,i = 1,2,3等效于(i = 1),2,3
若将”i = 1,2,3”改成”i = (1,2,3)”,i的值为3。

第二十五题(Reverse Polish Notation)

The following is a piece of code which implements the reverse Polish Calculator. There is a(are) serious(s) bug in the code. Find it(them) out!!! Assume that the function getop returns the appropriate return values for operands, opcodes, EOF etc..
#include <stdio.h>
#include <stdlib.h> #define MAX 80
#define NUMBER '0' int getop(char[]);
void push(double);
double pop(void);
int main()
{
int type;
char s[MAX]; while((type = getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
push(pop() - pop());
break;
case '/':
push(pop() / pop());
break;
/* ...
* ...
* ...
*/
}
}
}
题目讲解:(不确定)
减法的减数和被减数反了,除法的除数和被除数反了。

C puzzles详解【21-25题】的更多相关文章

  1. C puzzles详解【51-57题】

    第五十一题 Write a C function which does the addition of two integers without using the '+' operator. You ...

  2. C puzzles详解【46-50题】

    第四十六题 What does the following macro do? #define ROUNDUP(x,n) ((x+n-1)&(~(n-1))) 题目讲解: 参考:http:// ...

  3. C puzzles详解【38-45题】

    第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...

  4. C puzzles详解【34-37题】

    第三十四题 The following times. But you can notice that, it doesn't work. #include <stdio.h> int ma ...

  5. C puzzles详解【31-33题】

    第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...

  6. C puzzles详解【26-30题】

    第二十六题(不会) The following is a simple program which implements a minimal version of banner command ava ...

  7. C puzzles详解【16-20题】

    第十六题 The following is a small C program split across files. What do you expect the output to be, whe ...

  8. C puzzles详解【13-15题】

    第十三题 int CountBits(unsigned int x) { ; while(x) { count++; x = x&(x-); } return count; } 知识点讲解 位 ...

  9. C puzzles详解【9-12题】

    第九题 #include <stdio.h> int main() { float f=0.0f; int i; ;i<;i++) f = f + 0.1f; if(f == 1.0 ...

随机推荐

  1. Python标准库的学习准备

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python标准库是Python强大的动力所在,我们已经在前文中有所介绍.由于标准 ...

  2. iOS推送通知的实现步骤

    一.关于推送通知 来源:http://blog.csdn.net/enuola/article/details/8627283 推送通知,也被叫做远程通知,是在iOS 3.0以后被引入的功能.是当程序 ...

  3. SQL查询包含汉字的行

    1.查询字段首位为汉字 2.查询字段包含汉字(任意位) SELECT * FROM 表名 WHERE 字段 LIKE '%[吖-座]%' --[吖-座]是中文字符集第一个到最后一个的范围

  4. 页面设计--Tree目录树

    Tree目录树控件属性: 根据数据集合来配置相应的信息 加载模式有自动加载.自定加载 web中显示效果图:

  5. 关于WinCE流接口驱动支持10以上的端口号(COM10)

    一般情况下,WinCE流驱动的索引为0~9.应用程序中,通过CreateFile(_T("XXXN:"),…)打开对应的驱动,N也为0~9.这样看来,似乎在WinCE下同名流驱动个 ...

  6. JAVA设计模式之调停者模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述调停者(Mediator)模式的: 调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从 ...

  7. Sqoop2入门之导入关系型数据库数据到HDFS上

    需求:将hive数据库中的TBLS表导出到HDFS之上: $SQOOP2_HOME/bin/sqoop.sh client sqoop:> set server --host hadoop000 ...

  8. 利用UDP19端口实施DOS攻击的真实案例

    昨天在一个用户现场发现了一个利用UDP19端口对互联网受害者主机进行DOS攻击的真实案例.这个情况是我第一次见到,个人认为对以后遇到此类情况的兄弟具有参考价值.有必要做一个简单的分析记录. 在此次的分 ...

  9. (旧)子数涵数·PS——替换颜色

    一,老规矩,下载素材(老样子,还是美美的少女) 二.打开PS,并打开素材. 三.复制一个图层,快捷键Ctrl+J(好习惯) 四.使用"替换颜色"命令,弹出对话框. 五.选择人物衣服 ...

  10. SDUT 3340 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两棵树 ...