You're getting mixed up with pointers to char and arrays.
In the case of your sentences struct, it doesn't contain two strings, it contains two
pointers to strings; the strings themselves will be elsewhere, and you have to allocate their memory as well.
Ignoring calloc for a second, let's just create an instance of a sentences struct:
1 2 3 4 5 6 7
|
typedef struct sentences{
char *sentences_FR;
char *sentences_AN;
sentences *next;
};
sentences aSentence;
|
The size of this instance will be sizeof(char*) + sizeof(char*) + sizeof(sentences*), the size of three pointers. On 32 bit architectures, this will equate to 4 + 4 + 4, so each sentences struct is 12 bytes long.
Now, at this point, both the sentences_FR and sentences_AN pointers are uninitialised and will be pointing to some random piece of memory, but more importantly they
don't contain strings. We have to create the strings ourselves and set the sentences_FR and AN pointers to point to them.
We can do this using strcpy, strdup or other str functions. For example, strdup will duplicate a string, allocating the required memory for us ...
1 2 3 4
|
char string1[] = "yo";
char string2[] = "blablabla";
aSentence.sentences_FR = strdup(string1);
aSentence.sentences_AN = strdup(string2);
|
We now have new copies of string1 and string2, pointed at by aSentence.sentences_FR and aSentence.sentences_AN.
Using our new-found understanding, we can now dynamically allocate a whole array of 42 (for example) sentences using:
sentences* tempptr = calloc(42, sizeof(sentences));
We now have 42 uninitialised sentences which we have to populate in the same way as our previous example.
The tempptr pointer will be pointing at the first sentence, so we can say:
1 2 3
|
sentences* entryPtr = tempptr; // entryPtr now points to 1st element of array
entryPtr->sentences_FR = strdup(aString);
entryPtr->sentences_AN = strdup(anotherString);
|
To move to the next element of the array, we just use pointer arithmetic:
entryPtr++;
. The compiler knows how big a sentences struct is, so it knows to shift the pointer by that many bytes when you increment it. If you want to access the 4th entry directly, for example, you'd say
entryPtr = tempptr + 3;
Cheers
Jim