所以我相信这个问题已经回答了很多次,但是我无法看到如何解决我的情况。我把我的节目的片段,包含我的警告生成代码:传递指向字符串的指针,不兼容的指针类型
#include <stdio.h>
#include <stdlib.h>
inputData(int size, char *storage[size])
{
int iterator = -1;
do {
iterator++;
*storage[iterator] = getchar();
} while (*storage[iterator] != '\n' && iterator < size);
}
main()
{
char name[30];
inputData(30, name);
}
警告消息:
$ GCC的text.c 的text.c:在函数 '主': 的text.c :18:5:warning:从不兼容的指针类型[默认启用]传递'inputData'的参数2 inputData(30,name); ^的text.c:4:1:注:应为 '字符**',但参数的类型的 '字符*' inputData(INT大小,字符*存储[大小])
编辑:
好的,所以我设法使用一些语法来解决我的问题。我仍然不会;不介意听到有人比我更了解情况,为什么这是需要的。这里是我改变的:
#include <stdio.h>
#include <stdlib.h>
inputData(int size, char *storage) // changed to point to the string itself
{
int iterator = -1;
do {
iterator++;
storage[iterator] = getchar(); // changed from pointer to string
} while (storage[iterator] != '\n'); // same as above
}
main()
{
char name[30];
inputData(30, name);
printf("%s", name); // added for verification
}
里面'inputData()',代码应该有3种原因而停止:1)'的getchar()''返回'\ n'' 2)'的getchar ()'返回'EOF' 3)没有更多空间。 – chux 2014-09-10 19:14:35