Re: Cannot return values of char variable



Pedro Pinto wrote:
>
>
> I can do that inside main. What i can't do is with divide:
>
> char divide(char search_string[], char *array[], char *delimiter)
>
> and return the string you printed! Can this be done?
>

As I understand, you want to create the array which holds the divided
tokens inside divide function and return it (instead of passing
char *array[] as argument). Please find below the code which does that.

Please note that this is my first answer to a question here.
So I would suggest you to wait, for any responses from the more
experienced members of c.l.c.

Also after posting my previous testcase, i have noticed few issues in
it. This version is better (I guess).


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char **divide(char search_string[], char *delimiter){
int loop;
char **array = malloc(50 * sizeof(char *));
array[0]=strtok(search_string,delimiter);

if(array[0]==NULL)
{
printf("No test to search.\n");
exit(0);
}

for(loop=1;loop<50;loop++)
{
array[loop]=strtok(NULL,delimiter);

if(array[loop]==NULL)
return array;
}

return array;
}

int main(void) {
int i = 0 ;
char **arr;

char input[] ="this,is,comma,separated,string";

arr = divide(input, ",");

printf("Divided contents\n");
for(i = 0; i < 50; i++)
{
if(arr[i]==NULL)
break;

printf("%s \n", arr[i]);
}

free(arr);
return 0;
}

Hope this helps.

--
Prasad

.



Relevant Pages

  • Re: First Program
    ... Find the mean value (sum divided by size) of the items in the array. ... double compute_mean(int *array, int size); ... until the next addition would overflow, then divide that sum by arr_length, ...
    (comp.lang.c)
  • Re: First Program
    ... 1) Find the mean value (sum divided by size) of the items ... What might happen if the array is real numbers ... double compute_mean(int *array, int size); ... I've precluded "0" as valid input in order to get around the divide ...
    (comp.lang.c)
  • Re: Efficient solution needed
    ... If you decide to divide it by 2, ... pre-compute the output and print it as required, ... int main(int ac, char* av) ...
    (comp.programming)
  • Re: An implementation where sizeof(short int) does not divide sizeof(int)
    ... sizeof(short int) does not divide sizeofor ... KCC a C compiler for PDP-10 had an option to use 7 bit char instead of the ...
    (comp.lang.c)
  • Re: Question on "if" statements...
    ... I had to go back through and divide it by ) to get the ... > above array declarations more succinctly. ... tableis a pointer to int. ...
    (comp.lang.c)