【发布时间】:2018-05-09 15:06:06
【问题描述】:
基本上,我正在做的是制作一个可以用来构建其他程序的基础程序,并且我有一个函数(称为输入),它返回从用户输入中收集的字符。现在,有两个错误说“return make integer without a cast”和“函数返回局部变量的地址”。我希望它能够做的是返回一个字符数组。
代码如下:
#import <stdio.h>
void cls() {
system(cls);
}
char input() {
char X[0];
int Y = 0;
char Z;
while (1 == 1) {
Y = Y + 1; //increment step
Z = getch(); //get character input
if (Z == ';') {
break;
} //break if stop character is pushed
char Q[Y];
int a;
for (a = 0; a < Y; a = a + 1) {
Q[a] = X[a];
} //pass the items of X to the larger Q array
Q[Y] = Z; //add new value to Q at end of array
char X[Y];
int b;
for (b = 0; b < Y + 1; b = b + 1) {
X[a] = Q[a];
} //pass the items of Q back to a larger X
printf(X);
}
return X;
}
int main() {
while (1 == 1) {
//stuff will go here later
}
return 0;
}
【问题讨论】:
-
'char* input()'
-
您有 多个 个名为
X的数组,每个数组彼此不同。然后返回顶级的X,它是 empty (实际上是不允许的)。此外,返回指向本地数组元素的指针将返回一个无效指针,因为数组超出范围并立即停止退出。 -
你还有很多其他的错误,比如
#import是什么?getch(or rather_getch) function 返回一个int(如果你想从中检查错误,这很重要,你应该这样做)。如果您尝试调用cls函数,它可能会崩溃。也许是时候让你退后几步了,get a good beginners book or two,然后重新开始。 -
那我应该如何修复代码呢?将值传递给Q后删除X,然后重新声明X,和Q一样吗?
-
“将值传递给 Q 后删除 X,然后重新声明 X”——这听起来根本不对。你应该得到a good C book 来学习。 C 不是一种靠猜测来学习的好语言。这可能是我在 SO 上看到的关于从函数返回数组的第五个问题。当然,您可以通过搜索找到一些答案。