1
typedef struct mensagem
{
    int sender ;
    int receiver ;
    char *text ;
} *Item ;

typedef struct node
{
    Item item ;
    struct node *next ;
} *link ;

typedef struct queue
{
    link head, tail ;
    int size ;
} *Queue ;

     void listsorted(Queue list)
    {
        Queue temp = list ;
        int s=temp->size ;
        char *sorted ;
        int i=0;

        sorted = (char*) malloc(sizeof(char));

        while ( i < s )
        {
            strcpy( sorted[i] , list->head->item->text ) ;
            list->head = list->head->next ;
            i++ ;
        }

        i=0;

        for ( i=0 ; i<s ; i++ )
        {
            printf("%s\n", sorted[i] ) ;
        }
    }

I want to sort alphabeticaly a queue, so I thought to copy the strings to an array and qsort that array. But i'm not even managing to make the array with the strings. I get an error on the strcpy . What am I doing wrong?

4
  • what error exactly are you getting? Is it at compile-time or at run-time? Commented May 12, 2014 at 14:31
  • Compile error on the first argument of strcpy Commented May 12, 2014 at 14:33
  • I think it should be char **sorted; cause you want to have an array of strings. Commented May 12, 2014 at 14:35
  • That solves it in the compiler, but i get a segmentation fault when i enter the while Commented May 12, 2014 at 14:38

2 Answers 2

1

You're not malloc()'ing both dimensions of the array. Take a look at the following code (untested):

 void listsorted(Queue list)
{
    Queue temp = list ;
    int s=temp->size ;
    char **sorted ; /* Changed here */
    int i=0;

    sorted = malloc((s + 1) * sizeof(char *)); /* Changed here */

    while ( i < s )
    {
        sorted[i] = malloc(strlen(list->head->item->text) + 1); /* Changed here */
        strcpy( sorted[i] , list->head->item->text ) ;
        list->head = list->head->next ;
        i++ ;
    }
    sorted[i] = NULL; /* Changed here. NULL terminate array */

    i=0;

    for ( i=0 ; i<s ; i++ )
    {
        printf("%s\n", sorted[i] ) ;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It compiles, but it is only printing the first string. EDIT: Nevermind, that was due to a problem in another part of the code, so it worked! Thanks.
1

sorted[i] is of type char, not of type char*. That's because you're only allocating a one-dimensional array (i.e., a string) and not a two-dimensional array (i.e., an array of strings). Further, you're mallocing 1 byte then attempting to copy the contents of list->head->item->text into it. This will be bad news even if you get this code to compile.

Make sure you always allocate the correct amount of memory, and make sure you always use strncpy instead of strcpy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.