2

Basically, I'm trying to sort an agenda with 3 names using selection sort method. Pretty sure the selection sort part is OK. The problem is that apparently my code can identify the [0] chars of the string, but cannot pass one string to another variable. Here is my code:

include <stdio.h>

typedef struct{
    char name[25];
} NAME;

int main(){

    int a, b;
    char x, y[25];

    static NAME names[]={
        {"Zumbazukiba"},
        {"Ademiro"},
        {"Haroldo Costa"}
    };

    for(a=0; a<4; a++){

        x = names[a].name[0];
        y = names[a];

        for(b=(a-1); b>=0 && x<(names[b].name[0]); b--){
            names[b+1] = names[b];
        }
        names[b+1].name = y;
    }
}

I keep getting this error message:

main.c:21:11: error: assignment to expression with array type y = names[a];

1
  • Because you are trying to assign to arrays like that. use this one y[a] = names[a] Commented Feb 13, 2020 at 14:39

2 Answers 2

1

There are at least two errors in your code, in the line flagged by your compiler. First, you can't copy character strings (or, indeed, any other array type) using the simple assignment (=) operator in C - you need to use the strcpy function (which requires a #include <string.h> line in your code).

Second, you have declared y as a character array (char y[25]) but names is an array of NAME structures; presumably, you want to copy the name field of the given structure into y.

So, instead of:

y = names[a];

you should use:

strcpy(y, names[a].name);

Feel free to ask for further clarification and/or explanation.

Sign up to request clarification or add additional context in comments.

Comments

1

For starters I do not see the selection sort. It seems you mean the insertion sort.

Arrays do not have the assignment operator. So statements like this

names[b+1].name = y;

where you are trying to assign an array are invalid.

And in statements like this

y = names[a];

you are trying to assign an object of the structure type to a character array.

Moreover the loops are also incorrect.

The array has only 3 elements. So it it is unclear what the magic number 4 is doing in this loop

for(a=0; a<4; a++){

and this loop

for(b=(a-1); b>=0 && x<(names[b].name[0]); b--){

skips the first iteration when a is equal to 0.

Here is a demonstrative program that shows how the selection sort can be applyed to elements of your array.

#include <stdio.h>
#include <string.h>

#define LENGTH  25

typedef struct
{
    char name[LENGTH];
} NAME;

int main(void) 
{
    NAME names[] =
    {
        { "Zumbazukiba" },
        { "Ademiro" },
        { "Haroldo Costa" }
    };

    const size_t N = sizeof( names ) / sizeof( *names );

    for ( size_t i = 0; i < N; i++ )
    {
        puts( names[i].name );
    }

    putchar( '\n' );

    for ( size_t i = 0; i < N; i++ )
    {
        size_t min = i;

        for ( size_t j = i + 1; j < N; j++ )
        {
            if ( strcmp( names[j].name, names[min].name ) < 0 )
            {
                min = j;
            }
        }

        if ( i != min )
        {
            NAME tmp = names[i];
            names[i] = names[min];
            names[min] = tmp;
        }
    }


    for ( size_t i = 0; i < N; i++ )
    {
        puts( names[i].name );
    }

    putchar( '\n' );

    return 0;
}

The program output is

Zumbazukiba
Ademiro
Haroldo Costa

Ademiro
Haroldo Costa
Zumbazukiba

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.