Thread: convert from data structure to array of strings & sort

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    convert from data structure to array of strings & sort

    I have data struct of Citizens I need to transfer them to array of string, then sort that array.
    This is what I wrote:
    Code:
    ConSort(PERSON arr[TOT])
    {
    	int i=0, j;
    	char *temp=NULL, **Lname[TOT];
    	while (arr[i].Lname)
    		strcpy (*Lname[i],arr[i].Lname);
    
    	for (i=0;i<TOT;i++)
    		for (j=0;j<TOT;j++)
    			if (strcmp(*Lname[j], *Lname[i])>0);
    			 {
    				temp=*Lname[j];
    				*Lname[j]=*Lname[j+1];
    				*Lname[j+1]=temp;
    			 };
    }
    In debbuger I get :
    Unhandled exception in "filename" access violation
    right in the begining of the function
    TIA,

    Ronen

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ronenk
    Code:
    	char *temp=NULL, **Lname[TOT];
    	while (arr[i].Lname)
    		strcpy (*Lname[i],arr[i].Lname);
    You don't have any space allocated, you just blindly start copying strings into wherever those pointers happen to point. This accesses random locations in memory, giving you an access violation.

    Try allocating space for the string before you copy to it.
    Code:
    while( arr[i].Lname )
    {
        *Lname[i] = malloc( strlen( arr[i].Lname ) + 1 );
        strcpy( *Lname[i], arr[i].Lname );
    }
    Furthermore, in your loop (and the one I have above), you never actually increment i, so it's going to hang on an infinite loop. Be sure you free everything you allocate.

    Also, know that as soon as this function ends, everything you've allocated is going to be lost, because it's in a global variable and you're not returning a pointer to it anywhere.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    fixed the things Quzah wrote & others. The new version:
    Code:
    char *ConSort(PERSON arr[TOT])
    {
    	int i=0, j;
    	char *temp=NULL, **Lname[TOT];
    
    	while( i<TOT )
    		{
    			*Lname[i] = (char*) malloc( strlen( arr[i].Lname ) + 1 );
    			strcpy( *Lname[i], arr[i].Lname );
    			i++;
    		}
    
    	for (i=0;i<TOT;i++)
    		for (j=0;j<TOT;j++)
    			if (strcmp(*Lname[j], *Lname[j+1])>0);
    			 {
    				temp=*Lname[j];
    				*Lname[j]=*Lname[j+1];
    				*Lname[j+1]=temp;
    			 };
    			 return **Lname;
    			 free(Lname);
    }
    still same old error in the begining of the first while loop.

    Ronen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using qsort to sort an array of strings
    By MSF1981 in forum C Programming
    Replies: 31
    Last Post: 05-17-2009, 01:31 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. bubble sort array of strings
    By the_head in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 05:15 PM