Thread: problem with struct

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    problem with struct

    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("**********");
    	}
    }
    Last edited by megazord; 12-25-2009 at 08:56 AM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The code doesn't even compile ... Where is cname defined for example? And the way you've used strcpy makes no sense. It's strcpy(DESTINATION, SOURCE), and I suspect you're using it the other way around.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Quote Originally Posted by Memloop View Post
    The code doesn't even compile ... Where is cname defined for example? And the way you've used strcpy makes no sense. It's strcpy(DESTINATION, SOURCE), and I suspect you're using it the other way around.
    I edit the program.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    strcpy(temp->info->title, cname);
    Where is temp->info->title pointing? Who knows, you haven't initialized it. You should assign the malloc'd memory pointer to the temp->info->title pointer before you try to copy data to that memory location with strcpy.

    The error in the other function is identical.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM