Re: Cannot return values of char variable
- From: Prasad <noname@xxxxxxxxx>
- Date: Fri, 03 Nov 2006 19:51:47 +0530
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
.
- Follow-Ups:
- Re: Cannot return values of char variable
- From: Richard Heathfield
- Re: Cannot return values of char variable
- References:
- Cannot return values of char variable
- From: Pedro Pinto
- Re: Cannot return values of char variable
- From: T.M. Sommers
- Re: Cannot return values of char variable
- From: Pedro Pinto
- Re: Cannot return values of char variable
- From: mark_bluemel
- Re: Cannot return values of char variable
- From: Pedro Pinto
- Re: Cannot return values of char variable
- From: Prasad
- Re: Cannot return values of char variable
- From: Pedro Pinto
- Cannot return values of char variable
- Prev by Date: scanf hangs when taking multiple inputs in a char array
- Next by Date: Re: scanf hangs when taking multiple inputs in a char array
- Previous by thread: Re: Cannot return values of char variable
- Next by thread: Re: Cannot return values of char variable
- Index(es):
Relevant Pages
|
|