Look at the code below and compare it with your program, make sure that you find out what is wrong with your program.
#include <stdio.h> #include <string.h> typedef struct car{ char *model; int year; }Car; Car * newCar(char *, int ); int main() { Car *benz = newCar("S-class",1990); printf("\nModel = %s\n",benz->model); printf("\nYear = %d\n",benz->year); } Car * newCar(char *model, int year) { Car *new = malloc(sizeof(Car)); new->year = year; new->model = malloc(strlen(model)); strcpy(new->model, model); return new; }
source share