Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么代码在C中显示为空白而不是字符串?_C_String_Char_Integer - Fatal编程技术网

为什么代码在C中显示为空白而不是字符串?

为什么代码在C中显示为空白而不是字符串?,c,string,char,integer,C,String,Char,Integer,我希望代码显示用户输入的字符串,但代码显示为空白。有时,它还显示null和整数,而不是字符串 下面是代码片段: typedef struct list{ char DATA[30]; int last; } LIST; LIST L; int main(){ char x printf("Enter name:");scanf(" %s",&x);insert(x); display();getch(

我希望代码显示用户输入的字符串,但代码显示为空白。有时,它还显示null和整数,而不是字符串

下面是代码片段:

typedef struct list{
    char DATA[30];
    int last;
} LIST;

LIST L;

int main(){
    char x

    printf("Enter name:");scanf(" %s",&x);insert(x);

    display();getch();
}

void insert (char x){
    L.last++;
    L.DATA[L.last] = x;
}

void display(){
system("cls");
printf("The list contains:\n");
    for (int i=0;i<=L.last;i++){
    printf("%d. %s\n",i+1,L.DATA[i]);
    }
}
typedef结构列表{
字符数据[30];
int last;
}名单;
清单L;
int main(){
字符x
printf(“输入名称”);scanf(“%s”和&x);插入(x);
display();getch();
}
无效插入(字符x){
L.last++;
L.DATA[L.last]=x;
}
无效显示(){
系统(“cls”);
printf(“列表包含:\n”);

for(int i=0;iC没有字符串类型,只有字符类型。当您声明
char x
保存字符串时,这是单个字符,而不是整个字符串。我已将其替换为
char x[100]
-它声明了一个包含100个字符的数组来保存字符串。如果不够长,您可以增加它。然后我将
insert()
LIST
更改为使用
char*
(指向字符数组的指针)而不是单个
char


您的列表出现了
insert()
函数未写入第一个列表元素的问题。我通过在递增
L.last
之前写入列表来修复此问题。我还通过更改
scanf(“%s”、&x”)来修复
display
函数打印不存在的列表元素的问题
hmm.
%s
用于扫描字符串,但
x
是ant integer。使用
%d
扫描整数。确保使用%s显示字符串,而不是%d编辑:剪切
printf(“%d”)%d\n,i+1,L[MAX]。数据[i]);
再次:
%d
代表整数,
%s
代表字符串。您可能需要
printf(“%d”)%s\n,i+1,L[i+1]。数据);
(或者只是
i
而不是
i+1
)也请注意:
L[MAX]
是越界访问,即在数组之外。您的问题没有正确的答案。它不包含任何必需的内容:输入;预期或实际输出;或解释问题中的整个代码片段预期要做什么。我尝试了代码,但这些项用当前项覆盖了前面的项。因此我得到了结果例如:1.Ron 2.Ron 3.Ron 4.Ron代码应该这样做:1.Ron 2.Ram 3.Joy 4.Kath您应该尝试使用
malloc()
来分配您的字符数组,而不是
char x[100]
。这样在函数退出时它就不会被释放。我认为这是您的问题。您还可以考虑使用
getline()
正如我前面提到的。
typedef struct LIST
{
    char* DATA[30];
    int last;
} LIST;

LIST L;

void insert (char* x);
void display();

int main(){
    char x[100]; // Declare a character array to hold our string.
    printf("Enter name: ");
    scanf("%s", x); // Read the string into the array.
    insert(x); // Pass the memory address of the array to insert().
    display();
    getch();
}

void insert (char *x)
{
    L.DATA[L.last] = x; // Put the memory address of the array into the list.
    L.last++; // Increment the list counter.
}

void display()
{
    printf("The list contains:\n");
    for (int i=0; i < L.last; i++)
    { // Loop over the list, printing
        printf("%d. %s\n", i+1, L.DATA[i]);
    }
}