Reputation: 69
typedef struct listaDocente nodoDocente;
struct listaDocente{ //teacherList
char Rut[12]; //ID
char Nombre[50]; //Name
char Correo[70]; //Email
char Cod_Curso[6]; //Grade_Code
char Asignatura[15]; //Grade_Name
nodoDocente *siguiente; //Next (Node)
};
int main(){
nodoDocente *D = ((nodoDocente * )malloc(sizeof(nodoDocente)));
strcpy(D->Nombre, "Charlie");
strcpy(D->Rut, "18123456");
strcpy(D->Asignatura, "EDD");
strcpy(D->Cod_Curso, "INS123");
strcpy(D->Correo, "[email protected]");
printf("%s\n", D->Nombre);
printf("%s\n", D->Rut);
printf("%s\n", D->Asignatura);
printf("%s\n", D->Cod_Curso);
printf("%s\n", D->Correo);
return 0;
}
First, sorry if some words are in spanish. Im trying to print these values but i'm gettin a blank space.
Charlie
18123456
INS123
[email protected]
Where it should be printing out EDD, like this.
Charlie
18123456
EDD
INS123
[email protected]
Upvotes: 4
Views: 2327
Reputation: 2619
You have copied INS123
into Cod_Curso[6]
using strcpy
. Now strcpy
reads from src until it sees a '\0' and copies what was read(including '\0') into dest
. Hence here 7 characters will be copied to the buffer pointed by Cod_Curso
.
Now here is the problem. The allocated space in destination buffer is only of 6 characters. Hence copying 7 characters into it will cause a buffer overflow i.e. the extra characters will be copied into memory not belonging to Cod_Curso
but to the variable above it in the stack which here is Asignatura[15]
. So now the extra character '\0' is copied in Asignatura[0]
.
Now printf
with '%s'
prints until it sees a '\0'
in the passed buffer. Since Asignatura[0]
is '\0'
, Hence an empty string is printed.
Solution
It is better to use safe version of strcpy
like strncpy. Or you can increase the buffer size.
Upvotes: 4
Reputation: 5409
A string needs one extra byte to store \0
which makes an array of chars to be treated as string. You are violating this rule here,
char Cod_Curso[6];
strcpy(D->Cod_Curso, "INS123");
This overwrites \0
to memory assigned to someother variable and that causes a UB in your code.
If you are sure about the length of each string, you can use an exactly defined lengths for your char arrays or else you can declare all of them to be char*
and malloc each of them to store the strings you are giving or else provide sufficient array length (buffer
) for all.
Also, there is a memory leak in your code. You should be free
ing the memory allocated by using malloc
, calloc
and realloc
.
malloc
returns void*
which is implicitly promoted to any other pointer type in C, so you don't need to cast your malloc
.
Upvotes: 3
Reputation: 5009
You have declared :
char Cod_Curso[6];
and filled it with :
strcpy(D->Cod_Curso, "INS123");
But INS123
is followed by the terminating character for strings \0
. So instead of 6
characters as you might think, the string INS123
actually occupies 7
characters. As a result, you cannot expect the statement :
printf("%s\n", D->Asignatura);
to work properly as you invoke undefined-behavior with the previous line.
Solution : Declare Cod_Curso
with size 7
(or more), like this :
char Cod_Curso[7];
Also, take a look at this link on why not to cast the result of malloc.
Upvotes: 4
Reputation: 29178
Don't forget that a string in C always has a trailing \0
char. So your:
INS123
Is written
INS123\0
It's 7 char length not 6. So what you are doing is writing an additional \0
on the first char of Asignatura
and thus its length become 0.
To fix this issue you should declare Cod_Curso
bi enough:
char Cod_Curso[7]; //Grade_Code
Upvotes: 3