472,146 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

function returning array of strings

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

Nov 28 '05 #1
5 5341

shyam wrote:
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?

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.
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?

No. You cannot assign to an array object like that.
any suggestion will be most welcome

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.
thanks & regards
shyam


Nov 28 '05 #2

"shyam" <sh********@gmail.com> wrote
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?

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. */
}
Nov 28 '05 #3
John Bode <jo*******@my-deja.com> wrote:
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;


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.
Nov 28 '05 #4
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

Nov 29 '05 #5
shyam wrote:
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>
There is no such header. It should be:

#include <stdlib.h>
char **c = (char **)malloc(count * sizeof(ch));
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.
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
So you are modifying the string you passed in....

int main()
{
char *s = "hello how do u do";
char **t = tokenize(s);


.... 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 .

Nov 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tweaxor | last post: by
5 posts views Thread by damian birchler | last post: by
4 posts views Thread by Woody Splawn | last post: by
18 posts views Thread by svata | last post: by
20 posts views Thread by Andrew Morton | last post: by
8 posts views Thread by jodleren | last post: by
26 posts views Thread by aruna.mysore | last post: by
13 posts views Thread by Sri Harsha Dandibhotla | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.