as a part of a program aiming to store a dictionary and replace words according to it,i wrote a function which will basically split(using strtok
) a string to its words(seperated by spaces),and will store each word into an array of strings.
The code is as follows:
void StoreArr(char * original,char ** dest)
{
int i=0;
char * token =strtok(original, " ");
dest[i]=malloc(sizeof(token));
strcpy(dest[i],token);
++i;
while(token!=NULL)
{
dest[i]=malloc(sizeof(token));
strcpy(dest[i],token);
printf("%s",token);
token =strtok(NULL, " ");
++i;
}
}
i passed the following variables:
char * File = "";
File=malloc(Length(Text)*(sizeof(char)));
char ** Destination[Count(' ',File)];
the length of destination is the number of words.
once the program is run,it terminates itself without displaying the text
it is called using StoreArr(File,Destination);
edit:
int Length(FILE * file)
{
long result;
long origPos = ftell(file);//original start position of file
fseek(file, 0, SEEK_END);//move to end of file
result = ftell(file);//return value at end
fseek(file, origPos, SEEK_SET);//rewind to beginning
return result;
}
int Count(char a,char * b)
{
int i=0;
int count=0;
while(b[i]!='\0')
{
if(b[i]==a || b[i]=='\n')
++count;
++i;
}
return count+1;
}
?i get a warning "passing argument 1,2 of 'StoreArr' from incompatible pointer type [enabled by default]" Thanks in advance.
p.s:Breaking down string and storing it in array
the code in the last post is the same as mine,yet mine does not work.i suspect those 2 lines form a problem,i do not know why:
dest[i]=malloc(sizeof(token));
strcpy(dest[i],token);