在C中的问题读数在C中用于动态数字输入



我是个菜鸟,在与C挣扎。我很难将头缠绕在指针,箭头->和DOT .符号。我正在研究一个具有1个结构的非常简单的程序,但是我不确定如何在不使用<cs50.h>标头的情况下让用户输入字符串(我需要了解这是更困难的问题,我也试图将我的头缠住)。

//#include <cs50.h>
#include <stdio.h>
#include <string.h>
// create a struct, call it pupils
typedef struct
{
    char *name;
    char *dorm;
}
pupils;
int main(void)
{
    // Allocate space for students - generally dynamic, here static
    int enrollment = 2;
    // create variable 'idinfo', which contains (enrollment) number of structs of type pupils  
    pupils idinfo[enrollment];
    // Prompt for students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
        // Not the way to do this given various error codes.....
        char idinfo[i].Name[20];
        char Dorm[20];
        // gets idinfo[i].Name
        scanf("%s", idinfo[i].Name);
        //idinfo[i].name = printf("%s", Name);
        // Below syntax works fine when <string.h> header included
        idinfo[i].dorm = get_string("Dorm: ");
    }
    // Print students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
        printf("%s is in %s.n", idinfo[i].name, idinfo[i].dorm);
    }
}

假设您不想使用CS50魔术,则必须分配和划分字符串。从您的代码中,我还将假设输入字符串应该比20个字符短。以下代码甚至不能确保它是正确的,但也不会崩溃,而只会给出意外的结果。

代码可能是:

int main(void)
{
    // Allocate space for students - generally dynamic, here static
    int enrollment = 2;
    // create variable 'idinfo', which contains (enrollment) number of structs of type pupils  
    pupils idinfo[enrollment];
    // Prompt for students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
        // allocate memory for idinfo[i] pointers:
        idinfo[i].name = malloc(20);
        scanf("%19s", idinfo[i].name);
        idinfo[i].dorm = malloc(20);
        scanf("%19s", idinfo[i].dorm);
    }
    // Print students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
        printf("%s is in %s.n", idinfo[i].name, idinfo[i].dorm);
    }
    // free allocated memory
    for (int i = 0; i < enrollment; i++)
    {
        free(idinfo[i].name);
        free(idinfo[i].dorm);
    }
}

您可能想要这样的东西:

#include <cs50.h>    // needed for get_xxx functions and other cs50 stuff
...
// Prompt for students' names and dorms
for (int i = 0; i < enrollment; i++)
{
   idinfo[i].name = get_string("Name: ");
   idinfo[i].dorm = get_string("Dorm: ");
}

,但这应该在您提供的文档中介绍。

我的建议根本不使用scanf,而是仅使用CS50 get_xxx输入方法。

和当心:CS50 string不是真正的"字符串"类型,而是与char *

感谢Serge,我能够获得我的代码编译,这是下面的。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// create a struct, call it pupils
typedef struct
{
    char *name;
    char *dorm;
}
pupils;
int main(void)
{
    // Allocate space for students - generally dynamic, here static
    int enrollment = 2;
    // create variable 'idinfo', which contains (enrollment) number of structs of type pupils  
    pupils idinfo[enrollment];
    // Prompt for students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
    // allocate memory for idinfo[i] pointers:
        idinfo[i].name = malloc(20);
        idinfo[i].dorm = malloc(20);
        printf("Enter student %d's Name ", i+1); 
        scanf("%19s", idinfo[i].name);
        printf("Enter student %d's dorm ", i+1);
        scanf("%19s", idinfo[i].dorm);
    }
    // Print students' names and dorms
    for (int i = 0; i < enrollment; i++)
    {
        printf("%s is in %s.n", idinfo[i].name, idinfo[i].dorm);
    }
    // free allocated memory
    for (int i = 0; i < enrollment; i++)
    {
        free(idinfo[i].name);
        free(idinfo[i].dorm);
    }
}

最新更新