C 当试图将输入字符分配给我的结构时,程序会崩溃

C 当试图将输入字符分配给我的结构时,程序会崩溃,c,scanf,C,Scanf,作为一名编程新手,我正在尝试制作一个程序,该程序将从用户处获取输入,并将其保存到一个结构中,该结构稍后将保存到一个文件中。除了用户输入部分外,其他所有部分都正常工作,但前两部分按预期工作(名称和姓氏为数组) 最后,我在main()中做了一个占位符,但我不知道这是否是必需的 strcpy(user_profile.first_name, "placeholder"); strcpy(user_profile.last_name, "placeholder"); user_profile.gende

作为一名编程新手,我正在尝试制作一个程序,该程序将从用户处获取输入,并将其保存到一个结构中,该结构稍后将保存到一个文件中。除了用户输入部分外,其他所有部分都正常工作,但前两部分按预期工作(名称和姓氏为数组)

最后,我在main()中做了一个占位符,但我不知道这是否是必需的

strcpy(user_profile.first_name, "placeholder");
strcpy(user_profile.last_name, "placeholder");
user_profile.gender = 't';
user_profile.age = 5;
user_profile.height = 5;
user_profile.weight = 5;
赋值发生在一个函数内部,该函数由main()调用,如果该函数有任何相关性的话

占位符也是在main中创建的


**更新**

问题已经解决(看答案1) 然而,当尝试使用facanf从文件中扫描回来时,出现了一个新问题,结果如下

我尝试过使用
fscanf()
,但是前两行读取无效

    /* this function will load the user profile if such a profile exists */
    void user_profile_loader(struct profile_info user_profile)
    {
    FILE *file_pointer;
    int i;
    file_pointer = fopen("test.txt", "r");

    fscanf(file_pointer, user_profile.first_name);

    fscanf(file_pointer, user_profile.last_name);

    fscanf(file_pointer, &(user_profile.gender));

    fscanf(file_pointer, &(user_profile.age));

    fscanf(file_pointer, &(user_profile.height));

    fscanf(file_pointer, &(user_profile.weight));

    printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
    user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
然而,我需要帮助说明哪些行应该被读取(1到6),这是我第一次尝试使用的

    /* this function will load the user profile if such a profile exists */
    void user_profile_loader(struct profile_info user_profile)
    {
    FILE *file_pointer;
    int i;
    file_pointer = fopen("test.txt", "r");

    fscanf(file_pointer, 1, user_profile.first_name);

    fscanf(file_pointer, 2, user_profile.last_name);

    fscanf(file_pointer, 3, &(user_profile.gender));

    fscanf(file_pointer, 4, &(user_profile.age));

    fscanf(file_pointer, 5, &(user_profile.height));

    fscanf(file_pointer, 6, &(user_profile.weight));

    printf("%s \n%s \n€c \n%d \n%d \n%lf", user_profile.first_name, user_profile.last_name,
    user_profile.gender, user_profile.age, user_profile.height, user_profile.weight);
}
两个都不起作用,而当前的一个(第一个)给了我一个错误

In file included from introduktion.c:1:0:
(my directory) stdio.h:385:15: note: expected 'const char * restrict' but argument is of type 'int'
int __cdec1 fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;

传递fscanf的参数2将指针从整数转换为不带强制转换的整数 所以它不知道它得到了什么输入?

在代码中

scanf(" %c", user_profile.gender);
应该是

scanf(" %c", &(user_profile.gender));
             ^
您需要提供变量的地址作为格式说明符的参数


有关
scanf()

的更多信息,请阅读。您应该发布代码,而不是添加屏幕截图。请以文本形式显示您的代码,不要照片。这不是Instagram。很抱歉:-)刚刚找到了正确的输入代码的方法,fll的帖子已经被编辑:-)我把它叫做“性别”而不是“性别”(让我们在StackAntropology上讨论一下)。@MOehm,它现在被修复了:-)在格式化过程中遇到了一些问题,问题已经解决了,但是,我在尝试加载文件时遇到了一个新错误,我应该创建新帖子还是编辑当前帖子?:-)很有魅力,谢谢!你能解释一下为什么会这样吗?是因为参数被多次传递,它是一个指针,然后要在结构中选择一个特定的子对象,我需要将它分配给addres(因为它是指针?还是我只是在说狗屎,对这个仍然很陌生:-)@NikolajLjørring“…此类转换的结果(如果有)存储在格式后面的指针参数所指向的位置。“我明白了!这是一个bucnh,我遇到了一个我无法理解的新问题,编译器从stdio.h打印出了一些东西,因为我在90分钟内不能问新问题,你介意看一下以下内容吗?
,在Introduction.c中包含的文件中:1:0:(我的目录)stdio.h:385:15:注意:应为“const char*restrict”,但参数的类型为“int”int\u cdec1 fscanf(文件*\u restrict\u文件,const char*\u restrict\u格式,…)_uumingw_ATTRIB_DEPRECATED_SEC_WARN;
后接
从不兼容指针类型传递fscanf的参数2
应为'const char*restrict',但参数类型为int
,这表明fscanf只接受字符串,但我在这方面找不到任何东西
scanf(" %c", user_profile.gender);
scanf(" %c", &(user_profile.gender));
             ^