我已经在 k & r 书的帮助下学习了 c。非常令人兴奋,但我很早就遇到了麻烦,我不确定如何解决这个问题。
我正在尝试一些非常简单的示例代码,但出现以下错误。我不明白为什么,因为代码是直接从书上摘下来的。
main.c:11: warning: passing argument 2 of ‘sprintf’ makes pointer from integer without a cast
#include <stdio.h>
/* copy input to output; 1st version */
main() {
int i;
int power(int base, int n);
for (i = 0; i < 10; i++) {
sprintf("%d %d %d\n", i ,power(2, i), power(-3, i));
return 0;
}
}
int power(int base, int n) {
int i;
int p;
p = 1;
for (i = 1; i <= n; ++i)
p = p * base;
return p;
}
如果有人鼓励我继续前进,我将不胜感激。
最佳答案
sprintf
用于根据某种格式创建字符串。看起来你想要输出,所以你想使用 printf
。
此外,return 0;
不应包含在 for
循环中。这将导致程序在一次迭代后终止。
关于c - 简单的 c 错误使指针来自整数而不进行强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4249746/