Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 变量正在被printf中的后续变量覆盖_C_Loops_Variables_Output_Printf - Fatal编程技术网

C 变量正在被printf中的后续变量覆盖

C 变量正在被printf中的后续变量覆盖,c,loops,variables,output,printf,C,Loops,Variables,Output,Printf,我没有得到变量的完整单词,它被下一个元素覆盖 代码是这样的 #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> struct item { struct data { char content[10][20]; } details; struct item *next; }; typedef str

我没有得到变量的完整单词,它被下一个元素覆盖 代码是这样的

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
struct item
{
    struct data
    {
        char content[10][20];
    } details;
    struct item *next;
};
typedef struct item product;
void display(struct data *variable)
{
    int i = 0;
    struct data *v = variable;
    while (strcmp(v->content[i], "END") != 0)
    {
        printf("Element : %s \n", v->content[i]);
        i++;
    }
}
struct data *init_Tshirt()
{
    char usage_type[20];
    struct data *new_data = (struct data *)malloc(sizeof(struct data));
    printf("Enter The Usage type(Sports_Usage/Casuals_Usage): ");
    scanf("%s", usage_type);
    sprintf(new_data->content[0], "Usage Type = %s", usage_type);
    strcpy(new_data->content[1], "END"); // used as a deliminator
    return new_data;
}
struct data *init_Saree()
{
    char material_type[20];
    struct data *new_data = (struct data *)malloc(sizeof(struct data));
    printf("Enter The Material(Cotton_Material/Silk_Material): ");
    scanf("%s", material_type);  
    sprintf(new_data->content[0], "Material Type = %s", material_type);
    strcpy(new_data->content[1], "END"); // used as a deliminator
    return new_data;
}
int main()
{
    struct data *variable1 = init_Tshirt();
    display(variable1);
    struct data *variable2 = init_Saree();
    display(variable2);
}
我希望输出是

Enter The Usage type(Sports_Usage/Casuals_Usage): Sports_Usage
Element : Usage Type = Sports_Usage
Enter The Material(Cotton_Material/Silk_Material): Cotton_Material       
Element : Material Type = Cotton_Material
我不知道为什么输出字符串被覆盖。
任何帮助都将不胜感激

问题在于,当您使用sprintf将字符串写入字段内容的第一个元素(第一行)时,当字符串长度超过十九个字符时,您也会将其写入第二个元素(第二行)

您需要增加内容的长度并检查字符串是否合适,或者简单地使用snprintf截断它,snprintf接受一个额外的大小参数


您是否尝试过将
struct数据
定义移到
struct项
之外。它确实有效!但你能说得更准确些吗?我不明白第一个元素如何存储在下一个元素中,如果它真的存储在下一个元素中,它如何与第一个元素一起打印element@KrishnaAcharya这是因为在内存中,第二个元素(第二行)的第一个字节直接位于第一个元素(第一行)的最后一个字节之后。sprintf函数一直在写字符,直到写空字符为止。@KrishnaAcharya我添加了一个图像来说明发生了什么。非常感谢你的努力!非常感谢。
Enter The Usage type(Sports_Usage/Casuals_Usage): Sports_Usage
Element : Usage Type = Sports_Usage
Enter The Material(Cotton_Material/Silk_Material): Cotton_Material       
Element : Material Type = Cotton_Material