Re: How to Return an array of strings in C (char**)




<vignesh4u@xxxxxxxxx> wrote in message
I am trying to implement the Split function in C ie.
if i have a string: char* S="This is a test";
and if i try to split based on space ' ' it should return an array of
strings like:

{"This","is","a","test"} .

I tried to implement it as given below but am getting a segmentation
fault. I would really appreciate if some one could give me an answer
on this issue:

char** split(char str[],int*c) {
int start=0;
int end =0 ;
char** temp=NULL;
char** ptr;
int count=0;
int length = strlen(str);

This must be where you crashed, if you cut and pasted this code. The input must be null or not set up correctly.

//first counting the number of spaces
// to know the number of elements in the array
while (str) {
if (*str==' ') count++;
}

The reason is that you fail to increment str. Unless input is an empty string, this will run forever. Since the program didn't hang, probably the crash was above.

printf("count value is %d \n",count);
// I am doubt ful about the malloc too
ptr = (char**) malloc(sizeof(char)*(count+1));

you want (count + 1) * sizeof(char *).
It is an array of pointers, not of chars.

*c = count+1;

while(end<length) {
*ptr = str + start.
ptr = str + start;
What is length doing in this condition ?
if (str[end]!=' ' || length) {
end++;
}
this should surely be an else if.
if (str[end]!='\0') {// replacing spaces with \0
str[end]='\0';

Now you need to set *ptr to str + start. You also need to update start, and increment end.
{
This confused me. Much better to set temp before entering the loop.
if (!temp) temp= ptr;
ptr++;
}

return temp;
}

Thanks


Its nearly there. Generally the way to debug rotuintes like this is to slip in diagnostic printfs. For instance you could have printfed your input printf("***%s***\n", str) to see it was valid. The asterisks are there to pick up any funny spaces. The you could have printed "here" before entering the main loop to see if you actually got there, then str + start to see where you go to in the string on each pass.

.



Relevant Pages

  • (patch for Bash) regex conditional tests
    ... 'regex' are returned in array variable SUBMATCH. ... Skipping of positional parameters, array elements, string ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)
  • Re: parser needed
    ... static int tokenlen(const char *str, ... str - string containing expression ... ignores white space between tokens ...
    (comp.programming)
  • Re: passing char arrays by reference
    ... I want to pass an array of strings to a function: ... int stuff{ ... stuff is an incompatible pointer type. ... string type; a "string" is a data format, ...
    (comp.lang.c)
  • Re: String of numbers into to array of numbers
    ... then read that string and have the string show the ... individual numbers as in an array. ... int arryCnt = 0; ... qStr= cStr.substring); ...
    (comp.lang.java.help)
  • Re: read keyboard input and storing in an array?
    ... When in a loop, how do I simply ... The data returned by 'br.readLine' would always be a String so I've stored ... simply convert it to an int as follows: ... you could copy 'myInt' to your array. ...
    (comp.lang.java.help)