Connecting Tech Pros Worldwide Help | Site Map

function returning array of strings

shyam
Guest
 
Posts: n/a
#1: Nov 28 '05
Hi All

I have to write a function which basically takes in a string and
returns an unknown number( at compile time) of strings

i hav the following syntax in mind

char *tokenize(char *)[]

is it ok?

if yes then how do i call the function inside a code?

Suppose it returns 3 strings then

i must hav something like

char *c[3];


c = tokenize(/*the string*/)[];

is this the correct way to declare and use function returning arrays of
string?

any suggestion will be most welcome

thanks & regards
shyam

John Bode
Guest
 
Posts: n/a
#2: Nov 28 '05

re: function returning array of strings



shyam wrote:[color=blue]
> Hi All
>
> I have to write a function which basically takes in a string and
> returns an unknown number( at compile time) of strings
>
> i hav the following syntax in mind
>
> char *tokenize(char *)[]
>
> is it ok?
>[/color]

No. Functions cannot return array types. Functions can return
pointers to arrays, however:

char *(*tokenize(char*))[N] // return a pointer to an N-element array
of pointer to char

But that's not what you need, since you don't know how many substrings
you are returning.
[color=blue]
> if yes then how do i call the function inside a code?
>
> Suppose it returns 3 strings then
>
> i must hav something like
>
> char *c[3];
>
>
> c = tokenize(/*the string*/)[];
>
> is this the correct way to declare and use function returning arrays of
> string?
>[/color]

No. You cannot assign to an array object like that.
[color=blue]
> any suggestion will be most welcome
>[/color]

It's hard to make suggestions without giving away the answer
completely, but I'll try.

Since you don't know ahead of time how many substrings are going to be
in the parameter string, you are going to have to dynamically allocate
an array to hold the pointers to the substrings. The general form for
doing something like that is

char **arr;

arr = malloc(sizeof *arr * number_of_substrings)
if (arr)
{
size_t i;
for (i = 0; i < NUM_ARRAY_ELEMENTS)
{
arr[i] = next_substring; // type of arr[i] is char*
}
}

return arr;

Note how arr is declared; this will be the return type for the
function, as well as the type of the object in the caller.

Hopefully that was enough of a hint to get you headed in the right
direction.
[color=blue]
> thanks & regards
> shyam[/color]

Malcolm
Guest
 
Posts: n/a
#3: Nov 28 '05

re: function returning array of strings



"shyam" <shyam.geek@gmail.com> wrote[color=blue]
> I have to write a function which basically takes in a string and
> returns an unknown number( at compile time) of strings
>
> i hav the following syntax in mind
>
> char *tokenize(char *)[]
>
> is it ok?
>
> if yes then how do i call the function inside a code?
>
> Suppose it returns 3 strings then
>
> i must hav something like
>
> char *c[3];
>
>
> c = tokenize(/*the string*/)[];
>
> is this the correct way to declare and use function returning arrays of
> string?
>[/color]
No.

char **tokenize(char *string)
{
/* pass through the string, countin white space or other delimiters */
/* this tells you how many tokens - allocate an array of pointer to hold
them, which you return */

/* then pass thorugh again, pulling out all the tokens. Probably you want to
duplicate them
one at a time, holding the results in the array you return */
/* finally, caller probably wants some way of knowing how many tokens were
parsed. Either set an integer pointer, passed in, or set the last element of
the array to NULL. */
}


Christopher Benson-Manica
Guest
 
Posts: n/a
#4: Nov 28 '05

re: function returning array of strings


John Bode <john_bode@my-deja.com> wrote:
[color=blue]
> char **arr;[/color]
[color=blue]
> arr = malloc(sizeof *arr * number_of_substrings)
> if (arr)
> {
> size_t i;
> for (i = 0; i < NUM_ARRAY_ELEMENTS)
> {
> arr[i] = next_substring; // type of arr[i] is char*
> }
> }[/color]
[color=blue]
> return arr;[/color]

A note for OP: You will have to arrange to tell the caller how many
array elements there are, perhaps by returning a struct containing an
integer and a char **.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
shyam
Guest
 
Posts: n/a
#5: Nov 29 '05

re: function returning array of strings


Hi All

Based on ur suggestions i hav complied the below program but i am stuck
with some segmentation error
I have included in the comments wherever i have doubts
Please advise

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

#define SP ' '

char **tokenize(char *);


char ** tokenize(char *ch)// to tokenize the string based on space
{

int count = 0;
char *mrk = ch;

while(*ch != '\0')
{
if(*ch == SP)

count++;

ch++;

}

count = count + 2;

ch = mrk;

char **c = (char **)malloc(count * sizeof(ch));

int count2;
char *fst,*lst;

fst = lst = ch;

for (count2 = 0; count2 < count - 1; count2++)
{

lst = strchr(lst,SP);

*lst = '\0'; //the DDD crashes at this point

lst++;

c[count2] = fst;

fst = lst;
}

c[count - 1] = NULL;

return c;

}


int main()
{

char *s = "hello how do u do";

char **t = tokenize(s);

printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]
}


regards
shyam

Old Wolf
Guest
 
Posts: n/a
#6: Nov 29 '05

re: function returning array of strings


shyam wrote:
[color=blue]
> Hi All
>
> Based on ur suggestions i hav complied the below program but i am stuck
> with some segmentation error[/color]
[color=blue]
> I have included in the comments wherever i have doubts
> Please advise
>
> #include<stdio.h>
> #include<string.h>
> #include<malloc.h>[/color]

There is no such header. It should be:

#include <stdlib.h>
[color=blue]
> char **c = (char **)malloc(count * sizeof(ch));[/color]

Should be:

char **c = malloc(count * sizeof *c);

Don't cast the value returned by malloc.
Also you should check to see if the malloc succeeded.
[color=blue]
> char *fst,*lst;
> fst = lst = ch;
>
> for (count2 = 0; count2 < count - 1; count2++)
> {
> lst = strchr(lst,SP);
> *lst = '\0'; //the DDD crashes at this point[/color]

So you are modifying the string you passed in....
[color=blue]
>
> int main()
> {
> char *s = "hello how do u do";
> char **t = tokenize(s);[/color]

.... But string literals are not modifiable, so you cause undefined
behaviour. Change your main function to this:

char s[] = "hello how do you do";
char **t = tokenize(s);

If you are using GCC then you can detect this error by
using the compiler switch -Wwritable-strings .

Closed Thread