void allocate(char string[])
{
int nrCuv = 0, init_sz = strlen(string), counter = 0;
char delim[] = "\n";
for (int i = 0; i < strlen(string); i++) //Here I calculated the number of words from the file
{
if (string[i] == "\n")
{
nrCuv += 1;
}
}
nrCuv += 1;
char *tablou[nrCuv];
char *ptr = strtok(string, delim);
while (ptr != NULL) //Here I tried to split the initial char into words
{
tablou[counter] = ptr;
// printf("%s\n", ptr);
ptr = strtok(NULL, delim);
counter += 1;
}
for (int i = 0; i < nrCuv; i++)
{
puts(tablou[i]);
}
}
The variable string
has the contents of a file that has one word per line. I was trying to format the string variable so that I can access every word from within the tablou
variable, like this
tablou[1] = "foo"
tablou[2] = "bar"
For some reason that I don't understand. it doesn't seem to work properly at all. Sometimes I get Segmenation Faults, sometimes it only prints the first word of the file. What am I doing wrong?