I am still learning C and I’m having some trouble figuring out how to handle this. Well, I have two structs:
struct myStruct {
...
struct myString *text[5];
...
} allStructs;
struct myString {
char part[100];
};
The objective is to have allStruct[n] point to 5 different parts of a text divided into lines of 100 chars each. So I allocate the space:
allStructs = calloc(n, sizeof(allStructs));
Then, assume that I have a filled char text[500] that I want to divide into 5 parts, and have allStructs[n].text[n].part point at a given part of the text. Can anyone help me with how I proceed?
Short answer: you can not with this because you have not consider the ‘\0’ character to terminate each string.
Longer answer:
Change structs like that to have more flexibility:
The allocation should be:
So you have a pointer/array on n struct myStruct.
Then initialize all members of allStruct;
Now you have all vars initialized.
To copy your 500-chars long string into allStruct[n]:
This should work.