0

So i did this exercise some days ago but now i want to do it using pointers.

The problem is the error that i dont understand what it is: it says

error: assignment to expression with array type

So basically what the exercise says

we are give these names in a file in the given format

4 3
milano torino venezia
bari genova taranto
firenze napoli roma
bologna cagliari palermo

where 4 and 3 are the nr of rows and columns

We are to take this and print out in a single column the names in alphabetical order

AS i said i did this before but now i wanted to try doing this a bit differently and using pointers at the same time

EDIT: the code isnt finished yet i just complied it to see if there are any problems so ignore the last 2 for loops

heres what ive done so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100

typedef struct Ordered {
    char str[N];
} ordered;

int
main()
{
    int i = 0, j, n, m;
    char c1[20], c2[20], c3[20];
    ordered *v;
    FILE *file;

    file = fopen("file", "r");
    if (file == NULL) {
        printf("Error in the file...");
        exit(1);
    }

    fscanf(file, "%d %d", &n, &m);
    int k = n * m;

    v = (ordered *) malloc(k * sizeof(ordered));
    if (v == NULL) {
        fprintf(stderr, "Memory allocation error.\n");
        exit(EXIT_FAILURE);
    }

    while (fscanf(file, "%s %s %s", c1, c2, c3) && i < k) {
        v[i].str = (char *) malloc((strlen(c1) + 1) * sizeof(char));
        v[i + 1].str = (char *) malloc((strlen(c2) + 1) * sizeof(char));
        v[i + 2].str = (char *) malloc((strlen(c3) + 1) * sizeof(char));
        if (v == NULL) {
            if (v[i].str == NULL || v[i + 1].str == NULL || v[i + 2].str == NULL) {
                fprintf(stderr, "Memory allocation error.\n");
                exit(EXIT_FAILURE);
            }
        }

        strcpy(v[i].str, c1);
        strcpy(v[i + 1].str, c2);
        strcpy(v[i + 2].str, c3);
        i=+3;
    }

    char s[20];

    for (i = 1; i < k; i++) {
        s = v[i].str;
    }

    printf("\n\n");
    for (i = 0; i < n * m; i++)
        printf("%s\n", v[i].str);

    return 0;
}

Any idea on how to fix the given error?

Craig Estey
  • 30,627
  • 4
  • 24
  • 48
Severjan Lici
  • 371
  • 1
  • 10
  • 1
    `v[i].str` is an array, so `v[i].str = (char*) malloc` is an error. Change the type of `.str` to a `char *` if you want to do this type of allocation. – William Pursell Mar 26 '23 at 00:09
  • @WilliamPursell Damn. thanks for pointing it out. Completely forgot about that. – Severjan Lici Mar 26 '23 at 00:11
  • Arrays are not modifiable lvalues. They can't be assigned to. – Harith Mar 26 '23 at 00:16
  • 1
    Good job on checking the return of `malloc()`. Note that there's no need to cast it's result. It returns a generic `void *` that is implicitly converted to any other pointer type. The cast is redundant and just clutters the code. Consider checking the return value of `fscanf()` as well. – Harith Mar 26 '23 at 00:17
  • 1
    Aside: Look into standard `qsort()`. – Harith Mar 26 '23 at 00:43
  • You have a bug. You do: `i=+3;` This is actually: `i = 3;` I believe you want to _increment_ `i` by 3. The increment/set operator is `+=` and _not_ `=+` So, you want: `i += 3;` – Craig Estey Mar 26 '23 at 00:57
  • `fscanf(file, "%s %s %s", c1, c2, c3)` should be `fscanf(file, "%19s %19s %19s", c1, c2, c3) == 3` to avoid buffer overflow and insure all 3 conversions occurred. – chux - Reinstate Monica Mar 26 '23 at 03:57
  • Consider [`qsort()` for sorting](https://stackoverflow.com/a/67895815/3422102) which is a basic discussion on its use. Search `"[c] qsort struct"` for further specific information. – David C. Rankin Mar 26 '23 at 04:22

1 Answers1

0

The error you're encountering is because of this:

   for (i = 1; i < k; i++) {
       s = v[i].str;
   }

You cannot assign to an array, which s is.

You've shown earlier that you know how to use strcpy. That would be more appropriate in this instance.

Chris
  • 26,361
  • 5
  • 21
  • 42