0

So I've been new in structures so i tried to sort it like normal bubble sort but it won't work and also ti gives me error like "assignment with expression to array type"
I just want to sort names of cocktails in reverse way form 'z' to 'a'. I did rest of the code which works and i got stucked on this sort

I really tried every way I could think of so if someone can help me it would be great. Thanks :)

typedef struct {
    char name[20+1];
    char sast1[20+1];
    char sast2[20+1];
    char sast3[20+1];
} kokteli;

j=i;
int s;
int flag;
char *pom;

while(1) {
    flag = 0;
    for(s=0; s<j; s++) {
        if(k[s].name> k[s+1].name) {
            pom = k[s].name;
            k[s].name= k[s+1].name;
            k[s+1].name= pom;
            flag = 1;
        }
    } if(flag == 0){
            break;
    }
}
bool3max
  • 2,748
  • 5
  • 28
  • 57
  • Please provide a [mcve] – klutt Sep 09 '18 at 17:38
  • Please read [the help pages](http://stackoverflow.com/help), especially [about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Sep 09 '18 at 17:44
  • @user3121023 Thank you SIR!!!! – Marko Prtenjača Sep 09 '18 at 17:48
  • @Someprogrammerdude I will.Thanks :) – Marko Prtenjača Sep 09 '18 at 17:49
  • Note that although I've duplicated the question to ones about comparing strings, you also need to use functions (`strcpy()`, `memmove()`, `memcpy()` — and maybe `strncpy()` but be cautious about it) to copy strings. You're attempting pointer assignments on arrays — that doesn't work. You can't assign arrays; you can't assign pointers to an array (that is, given `char array[SIZE];`, you can't do `array = "DATA";`, even when the array is part of a structure). – Jonathan Leffler Sep 09 '18 at 19:18
  • Thank you very much! @JonathanLeffler – Marko Prtenjača Sep 10 '18 at 13:32

1 Answers1

0

So I've been new in structures so i tried to sort it like normal bubble sort but it won't work and also ti gives me error like "assignment with expression to array type"

I won't speak to what your code is actually trying to achieve but I will address the error you are getting. The compiler should be pointing out these two lines:

    k[s].name= k[s+1].name;
    k[s+1].name= pom;

So for clarity sake let's point out the fact that k is an array of kokteli structs and those structs contain members that are arrays (name, sast1, etc). k[s].name and k[s+1].name are both an array of char which in C is not assignable. You can modify the elements (char) of the array via pointers to the memory address for each char in the array. You would do that like this:

k[s].name[n] = //where n is an integer from 0 to 20, assign value of nth element here)

Let's break this statement down:

  1. k is a pointer to an array of kokteli structs
  2. k[s] is actually a pointer to the address of the kokteli struct located at the memory address offset from k by s (the exact same thing in C as (k+s))
  3. k[s].name is actually a pointer to the address of the member name of the kokteli struct located at offset s (can also be written in C as (k+s)->name or &k[s] //since the first member is name)
  4. k[s].name[0] is the contents of the 0th element of the member name of the kokteli struct located at the memory address offset from k by s (can also be written in C as *(k+s)->name)

This is why the compiler doesn't complain about this line:

pom = k[s].name;

Now the caveat here is that the name of an array in C actually refers to a memory address for the beginning of the contents of that array. This means that it is a pointer itself. So since pom and k[s].name are actually both char *, pom now points to what k[s].name is pointing at.

Your if clause here:

if(k[s].name > k[s+1].name){
    ...
}

Is actually comparing memory addresses as k[s].name and k[s+1].name both point to the first element in the array. The difference is that one is located at a memory offset that is sizeof(kokteli) greater than the other (that is k[s+1].name) so this statement will never be true. I've made some code for you here that will nail this home.

As other people in the comments have pointed out, you will actually want to use strcmp (need to #include <string.h>) to compare the elements of the two arrays of type char. Reference for strcmp is located here.

So your if clause should look something like this

if(strcmp(k[s].name, k[s+1].name) > 0){
    //k[s].name comes after k[s+1].name alphabetically
}

For more on how strcmp works, see this answer.

bigwillydos
  • 1,321
  • 1
  • 10
  • 15