MarceloveRei
MarceloveRei

Reputation: 23

Error: assignment to expression with array type while using selection-sort

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];

Upvotes: 2

Views: 146

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

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

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51835

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.

Upvotes: 1

Related Questions