Reputation: 1
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. 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; // initialize struct SYSTEMTIME 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
}
Upvotes: 0
Views: 1358
Reputation: 1912
Here n
is the local variable of the function input_cliente
. So the scope of n
is limited to the function body. It will be invalid after the function returns.
So you should either allocate it on the free store using malloc
:
struct cliente* tipo_cliente = (struct cliente*) malloc(sizeof(struct cliente));
Or let the function have an out parameter:
struct cliente* input_cliente(struct cliente* tipo_cliente)
{
// fill tipo_cliente here.
}
Upvotes: 1
Reputation: 57590
input_cliente
returns a pointer to a variable declared within the function. 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.
Upvotes: 1