Firstly, I apologize if this is a duplicate of some other question. I have spent several hours now playing around and attempting to figure out my problem. I may not have been searching with the correct terms as I am a novice with C.
I need to split up text using strtok
and store each value into a struct and store that struct in an array. I need the array's space to be dynamically allocated memory using malloc
/ realloc
.
The problem I am facing is that the last value I set is overwriting all previous values in my array. I am completely lost at this point. I have included a short example of my problem below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct textBit
{
int textID;
char** theWord;
int randInt;
} TextBit;
void printTextBit(TextBit bit);
int main()
{
int counter = 0;
char myText[] = "hello this is a bunch of text i am just writing for an example to test something";
TextBit** textBits;
textBits = malloc(sizeof(TextBit) * 16);
char* tok = strtok(myText, " ");
while(tok != NULL)
{
TextBit temp;
temp.textID = counter;
temp.randInt = 25;
char* tempWord = malloc(sizeof(char) * strlen(tok));
strcpy(tempWord, tok);
temp.theWord = &tempWord;
printf("%d %s\n", counter, tok);
//printTextBit(temp);
textBits[counter] = &temp;
counter++;
tok = strtok(NULL, " ");
}
for(int i = 0; i < counter; i++)
{
printTextBit(*textBits[i]);
}
}
void printTextBit(TextBit bit)
{
printf("TextBit: %s (#%d) - %d\n", *bit.theWord, bit.textID, bit.randInt);
}
This code outputs:
0 hello
1 this
2 is
3 a
4 bunch
5 of
6 text
7 i
8 am
9 just
10 writing
11 for
12 an
13 example
14 to
15 test
16 something
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25
TextBit: something (#16) - 25