I'm building a struct to hold a movie's information. When the movie is printed, it recursively prints the information of it's sequel.
struct movie
{
char name[28];
int year;
struct movie* sequel;
};
void print_movie(struct movie film)
{
printf("Name: %s \n", film.name);
printf("Year of Release: %d \n", film.year);
if (film.sequel == 0)
{
printf("%s jas no sequel, yet! \n\n", film.name);
}
else
{
printf("%s's sequel was... \n\n", film.name);
print_movie(*film.sequel);
}
}
int main(void)
{
struct movie jurassic;
struct movie lostworld;
strcpy(jurassic.name, "Jurassic Park");
jurassic.year = 1993;
jurassic.sequel = &lostworld;
All works as intended, until this part:
strcpy(lostworld.name, "The Lost World: Jurassic Park");
lostworld.year = 1997;
lostworld.sequel = 0;
print_movie(jurassic);
return 0;
}
I've counted the number of characters in 'The Lost World: Jurassic Park' (28) and used that as the maximum buffer. problem is, It's not dynamic and when I execute the program, It prints The Lost World: Jurassic Par- and makes an error sound.
If increase the char buffer, for example, to 29, I get Warning C4820: 'movie' : '3' bytes padding added after data member 'name' I'm working in Visual Studio, I've set the compiler to assess warnings as errors, for learning purposes and I've used _CRT_SECURE_NO_WARNINGS.
What's happening here? Should I use malloc for the char? Thanks