hello all,
I'm writhing a code with struct and I'm a littel bit confuse.
I get RUN TIME eroor and I don't why exactly.
the program Is fallin in the strcpy() int add studets and add course func.
some can tell me way? what am i doin worng?
Edit: I edit the program, and now its should compile.
I'm doing strcpy ok, cuse i want to read from cname and fullname and write to name and title.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student {
char *name;
int id;
struct clist *courses;
} student;
typedef struct course {
char *title;
int number;
struct slist *students;
} course;
typedef struct slist {
student *info;
struct slist *next;
} slist;
typedef struct clist {
course *info;
struct clist *next;
} clist;
//Prototype Preorderd functaion.
slist* add_student(slist *students, char *name, int id);
clist* add_course(clist *courses, char *title, int number);
void reg_student(slist *students, clist *courses, int id, int number);
void unreg_student(slist *students, int id, int number);
int is_registered(clist *courses, int id, int number);
int is_consistent(slist *students, clist *courses);
clist *add_course(clist *courses, char *title, int number){
clist *temp;
char *cname;
temp=(clist *)malloc(sizeof(clist));
cname=(char *) malloc (sizeof(char)*strlen(title));
strcpy(temp->info->title, cname);
temp->info->number=number;
temp->next=courses;
return temp;
}
slist *add_student(slist *students, char *name, int id){
slist *temp;
char *fullname;
temp=(slist *)malloc(sizeof(slist));
fullname=(char *) malloc (sizeof(char)*strlen(name));
strcpy(temp->info->name,fullname);
temp->info->id=id;
temp->next=students;
return temp;
}
void main(){
char name[1024]="eran bhanam";
int id_number=21613120;
char choice;
slist *p_stu;
p_stu=(slist *)malloc(sizeof(slist));
printf("Choose:\n\tadd (s)tudent\n\tadd (c)ourse\n\t(r)egister student"
"(u)nregister student\n\t(i)s registered\n\tis consisten(t)\n\t(q)uit\n");
scanf("%c",&choice);
switch(choice){
case 's':
printf("Adding new student.\nStudent name:");
//name = (char*)malloc(sizeof(char)*strlen(str)) ;
//strcpy(name,str);
//gets(name);
printf("\n");
printf("Student ID: ");
//scanf("%d",&id_number);
p_stu=add_student(p_stu,name,id_number);
printf("**********");
printf("Student: %s (%d)",p_stu->info->name,p_stu->info->id);
printf("Registered to: ");
printf("**********");
}
}