Reading a value of an array in a struct into the next indexed value?

Viewed 59

I'm trying to get the line:

allTowns[i - 1].startTown = allTowns[i].endTown;

to work, but whenever i run it i get an error code stating

"Error: Assignment to expression with array type"

Does anyone know why this is? My end goal is to run through the file energy.txt and put its contents into the array allTowns[1024], i got this to work, but now i would like to make every second line the same as the previous but with startTown, endTown swapped.

e.g. a file that looks like this:

Hull York 30

Manchester Derby -21

would create a struct i can print that looks like this:

Hull York 30

York Hull 30

Manchester Derby -21

Derby Manchester -21

Thank you very much for any help anyone can provide or suggestions, i'm new to C, the code I've pasted below is my code, but I have missed off the includes (<stdio.lib> and <stdio.h>). as it is in a header file

struct intertownEnergy {
  char startTown[256];
  char endTown[256];
  int energyExpended;

}
allTowns[1024], reverseTowns[1024], readInTowns[8];

int openEnergyFile(void) {
  FILE * energyFilePtr = fopen("energy.txt", "r");
  int i = 0;
  if (energyFilePtr == NULL) {
    printf("the file doesn't exist");
    return 0;
  }

  while (!feof(energyFilePtr)) {
    fscanf(energyFilePtr, "%s %s %d\n", & allTowns[i].startTown, & allTowns[i].endTown, & 
allTowns[i].energyExpended);
    printf("Printing the file: %s %s %d\n", & allTowns[i].startTown, & allTowns[i].endTown, 
allTowns[i].energyExpended);
    i++;
    allTowns[i - 1].startTown = allTowns[i].endTown;
  }
  printf("\n");
  //printf("Printing town middle test: %s %s %d\n", &allTowns[19].startTown, &allTowns[12].endTown, allTowns[12].energyExpended); WORKING TEST

  return 0;
}
3 Answers

You can't copy arrays with =. strncpy works.

 strncpy(allTowns[i - 1].startTown, allTowns[i].endTown, 255)[255] = 0;

Here the string is 255 characters long and we use the last spot only for the null terminator.

There really should be a dupe for this but I failed to find it.

In C, an array is a starting address and a size in memory.

When you write like this you have two different arrays in memory and you are trying to move the contents of one to the other.

allTowns[i - 1].startTown = allTowns[i].endTown;

This is not allowed in C, at least not with that syntax.

Instead you need to copy the contents of endTown to startTown, this can be done with strncpy or memcpy

memcpy(allTowns[i - 1].startTown, allTowns[i].endTown,sizeof(allTowns[i - 1].startTown))

As pointed out before you should use strcpy() function in <string.h> library.

The reason however, because strings in C are pointers, more specifically, they point to the address of the first character of the string. Using (=) operator means that you are assigning the address of allTowns[i].endTown to this pointer allTowns[i - 1].startTown which is not allowed since it's almost similar to doing this allTowns[i].endTown = 0x7ffff3a9ae94, b/c it is the operating system's job to assign memory addresses to pointers.

Related