It's been a while that I haven't written programs in C. I am used to write code in C#.
So, I want to split the user string input into an array of strings using a delimiter. I did this, but when I want to fetch the array I have a segmentation fault. As an example, I want just to print one element of the array.
I have checked on the net, but nothing worked.
Any hints ?
Thanks
#include<stdio.h>
#include<string.h>
int main ()
{
char function[] = {};
char * pch;
int cpt = 0;
int nb_terms = 0;
printf("Entrez le nombre de termes :\n");
scanf("%d", &nb_terms);
char word[nb_terms];
printf("Entrez votre fonction en n'utilisant que les 5 caractères suivants (a,b,c,d et +) :\n");
scanf("%s", &function);
pch = strtok (function,"+");
while (pch != NULL)
{
word[cpt++] = pch;
printf ("%s\n",pch);
pch = strtok (NULL, "+");
}
printf ("%s\n",&word[1]);
return 0;
}
char function[] = {};what is this supposed to mean?functionis of a fixed size of none. Use a pointer to a char instead and allocate with malloc or establish a big enough size for the array.