简体   繁体   English

用用户输入和通过指针访问来填充结构

[英]Populate structure with user input and access through pointer

I´m trying to create a structure populated by the user and accessed through a pointer. 我正在尝试创建一个由用户填充并通过指针访问的结构。

As it stands right now I don´t get compiler errors but it won't take input correctly, I need to input the second variable twice, and the output is garbage. 就目前而言,我没有遇到编译器错误,但不会正确输入,我需要两次输入第二个变量,并且输出是垃圾。

I´m trying to get pointers and structures down before moving on to linked lists, any help would be appreciated. 我在尝试移至链表之前先确定指针和结构,对您的帮助将不胜感激。

//struct date

struct data {    
    int dia;
    int mes;
    int ano;
};

//struct client

struct cliente {        
    char nome[15];
    int num_conta;
    float saldo;
    struct data dia_d_mes;          
};


// function that returns pointer to struct populated by user

struct cliente *input_cliente()
{    
    struct cliente *tipo_cliente, n;        
    SYSTEMTIME st;
    GetSystemTime (&st);

    tipo_cliente = &n;

    printf ("Nome cliente:");   
    gets (tipo_cliente->nome);
    //fflush (stdin);

    printf ("Numero da conta:");
    scanf ("%d ", &tipo_cliente->num_conta);

    printf ("Saldo da conta:");
    scanf ("%f ", &tipo_cliente->saldo);

    tipo_cliente->dia_d_mes.dia = st.wDay;
    tipo_cliente->dia_d_mes.mes = st.wMonth;
    tipo_cliente->dia_d_mes.ano = st.wYear;

    return tipo_cliente;            // return pointer    
}

//print client

void print_cliente(struct cliente *tipo_cliente)
{    
    printf  ("%s", tipo_cliente->nome);
    printf  ("\t%d", tipo_cliente ->num_conta);
    printf  ("\t%.2f", tipo_cliente ->saldo);
    printf  ("\t%d/%d/%d\n", tipo_cliente->dia_d_mes.dia, tipo_cliente->dia_d_mes.mes, tipo_cliente->dia_d_mes.ano);    
}


int main()
{    
    struct cliente *novo;       //declare a new struct pointer
    system ("color 17");
    system ("mode 70,10");

    novo = input_cliente();     //create new client

    system ("cls");

    printf ("Nome \t #conta \t Saldo \tData\n");
    printf ("============================================\n");

    print_cliente (novo);       //print new client    
}

I´ve been playing around with the code and changed the pointer to a normal structure input but keep having one constant problem. 我一直在研究代码,并将指针更改为正常的结构输入,但始终遇到一个恒定的问题。
When the second printf is desplayed and the int is entered, it doesn´t move to the next printf the cursor moves to a new line in the command prompt. 当第二个printf被显示并且输入int时,它不会移动到下一个printf,光标会移动到命令提示符下的新行。 Any idea would be apreciated, I´ve tryed different things with the pointer and without, but I´m running out of ideas. 任何想法都会被理解,在没有指针的情况下,我尝试了不同的方法,但是我的想法已经用完了。

// function that returns pointer to struct populated by user //返回指向用户填充的结构的指针的函数

struct cliente input_cliente() { struct cliente tipo_cliente; struct cliente input_cliente(){struct cliente tipo_cliente; // initialize struct SYSTEMTIME st; //初始化struct SYSTEMTIME st; GetSystemTime (&st); GetSystemTime(&st);

printf ("Nome cliente:");
gets (tipo_cliente.nome);                //accepts this value

printf ("Numero da conta:");
scanf ("%d ", &tipo_cliente.num_conta);  //also accepts this value 
                                           //after pressing enter goes to a empty line
printf ("Saldo da conta:");
scanf ("%f ", &tipo_cliente.saldo);  //the value stored in this variable is the 
                                       // value entered in the previous empty line
tipo_cliente.dia_d_mes.dia = st.wDay;
tipo_cliente.dia_d_mes.mes = st.wMonth;
tipo_cliente.dia_d_mes.ano = st.wYear;

return tipo_cliente;            // return pointer

} }

input_cliente returns a pointer to a variable declared within the function. input_cliente返回指向函数内声明的变量的指针。 However, once the function ends, the contents of that variable become undefined. 但是,一旦函数结束,该变量的内容将变得不确定。 You should either return an actual struct cliente (not a pointer) or use malloc to allocate memory for a struct cliente* that will last beyond the function's execution. 您应该返回实际的struct cliente (而不是指针),或者使用mallocstruct cliente*分配内存,该内存将持续到函数执行之外。

Here n is the local variable of the function input_cliente . 这里n是函数input_cliente局部变量 So the scope of n is limited to the function body. 因此, n的范围仅限于函数体。 It will be invalid after the function returns. 函数返回后将无效。

So you should either allocate it on the free store using malloc : 因此,您应该使用malloc在免费存储区中分配它:

struct cliente* tipo_cliente = (struct cliente*) malloc(sizeof(struct cliente));

Or let the function have an out parameter: 或者让函数具有out参数:

struct cliente* input_cliente(struct cliente* tipo_cliente)
{
     // fill tipo_cliente here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM