【发布时间】:2012-06-12 17:29:33
【问题描述】:
在介绍代码之前,先介绍一下背景知识:我有一个名为 math 的字符指针数组,现在,在 while 循环中,我尝试使用 @ 将数学中的值转换为 int 987654324@ 并将其保存在int 变量ai 中。没有编译错误。但是,当我尝试打印 ai 时,它并没有打印出来。程序以某种方式运行而不会崩溃。我不知道问题是什么,如果有问题程序应该在atoi 上崩溃,如果没有问题,那么它应该打印ai。
代码如下:
int c1 = 3; //the array contains 3 characters 1 2 3
int c2 = 0;
while(c2 < c1)
{
int ai;
ai = atoi(math[c2]);
// the array is valid, I have checked it time and again so is the content in array
write(STDOUT_FILENO,math[c2],1); //this works fine.
write(STDOUT_FILENO,&ai,sizeof(&ai));
//this doesn't print anything and somehow loop goes on to meet
//the condition.
c2++;
}
【问题讨论】:
-
什么是“math[c2]”?它是如何定义的?
-
它的 c2++;在倒数第二行,抱歉打错了。
-
math[c2]是一个字符指针数组,定义为:char *math[10];
-
阅读“写程序”,它接受一个字符串,因此如果 math 是一个字符串,那么 math[c2] 将是一个字符。 atoi 的输出将是一个整数。写入不接受整数。此外,您正在尝试使用“引用”ai,而不是 ai 本身。
-
我试过 printf("%d",&ai);这也不起作用。