I'm trying to store characters read from a file into an array of characters, but it ends up storing every succeeding character as well. For this code, I created a structure and a function to initialize the array.
From "Structures.h"
typedef struct (
int size;
char *elem;
} cvector;
From "Utilities.c"
cvector make_cvector (int size)
{ cvector temp;
temp.size = size;
temp.elem = calloc(size, sizeof(char));
return temp;
}
Then I'm trying to read a text file that says: "a b c"
In the body, I stated: (where nPtr is the pointer for opening the file)
cvector NodeID;
NodeID = make_cvector(3);
for(i=0;i<3;i++){
fscanf(nPtr,"%s", &NodeID.elem[i]);
printf("%s ",&NodeID.elem[i]);
}
This results in " a b c " But right after this loop, I typed another loop:
for(i=0;i<3;i++)
printf("%s ", &NodeID.elem[i]);
Resulting to "abc bc c" When in fact I want "a" "b" and "c" stored separately. There's probably something wrong with my initialization or pointers, but I've been trying to read up online to no avail. Where could the error be? Thank you!