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?