Like Tree1Likes
  • 1 Post By Subsonics

struct and strcpy

This is a discussion on struct and strcpy within the C Programming forums, part of the General Programming Boards category; hello! i'm trying to implement a function which build struct by adding each struct at a certain postion which been ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    struct and strcpy

    hello!
    i'm trying to implement a function which build struct by adding each struct at a certain postion which been set by "number of flight"

    the struct build like this:
    Code:
    typedef struct flight_{
    int number;
    char* date;
    char* origin;
    char* destination;
    struct passengerslist* passengers;
    }flight;
    
    typedef struct flightslist{
    flight* info;
    struct flightslist* next;
    }flights_list;
    the prototype of this function is the following:
    flights_list* add_flight(flights_list* flights, int number,char* date, char*
    origin, char* destination)

    i got trouble when i'm trying to copy the strings such date, origin and so
    to certain struct.

    i use strcpy.

    this is my code.
    Code:
    flights_list* add_flight(flights_list* flights, int number,char* date, char* origin, char* destination){
    	
    	flights_list *temp, *newCell, *head;
    	flight *temp_info;
    	
    	temp_info=(flight*)malloc(sizeof(flight));
    	newCell=(flights_list*)malloc(sizeof(flights_list));
    
    	
    
    	strcpy(temp_info->date,date);
    	strcpy(temp_info->destination,destination);
    	strcpy(temp_info->origin,origin);
    	temp_info->number=number;
    	newCell->info=temp_info;
    
    	if(flights==NULL){
    		newCell->next=flights;
    		return newCell;
    	}
    	
    	head=flights;
    	while(head != NULL || newCell->info->number > head->info->number){
    		temp=head;
    		newCell->next=head;
    		temp->next=newCell;
    		head=head->next;
    	}
    	head=temp;
    	return head;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your struct only contain pointers, you can not copy a string to a pointer unless it points to some memory that you own. Either allocate enough for each string, or if you have strdup() available use that, it will allocate enough memory and copy the string for you. If you don't have it, it might make sense to make such a function yourself.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    ok, so it can be sloved like this:
    temp_info->date=(char*)malloc(sizeof(255));
    and so on??

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by orid View Post
    ok, so it can be sloved like this:
    temp_info->date=(char*)malloc(sizeof(255));
    and so on??
    Sizeof(255) is probably not doing what you think, it makes more sense to use strlen(date) +1, for malloc, then you can use strcpy(). But as you do this more than once in a row, you can wrap both the strlen(), malloc() and strcpy() in a function that takes a string as argument and return a fresh new string.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using strcpy on a struct
    By ogglock in forum C Programming
    Replies: 2
    Last Post: 07-28-2011, 02:15 PM
  2. strcpy
    By 843 in forum C++ Programming
    Replies: 5
    Last Post: 05-09-2011, 12:18 PM
  3. Replies: 1
    Last Post: 06-19-2010, 07:42 AM
  4. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 11:34 AM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 03:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21