function returning array of strings

S

shyam

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
 
J

John Bode

shyam said:
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 = next_substring; // type of arr 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.
 
M

Malcolm

shyam said:
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. */
}
 
C

Christopher Benson-Manica

John Bode said:
char **arr;
arr = malloc(sizeof *arr * number_of_substrings)
if (arr)
{
size_t i;
for (i = 0; i < NUM_ARRAY_ELEMENTS)
{
arr = next_substring; // type of arr 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 **.
 
S

shyam

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
 
O

Old Wolf

shyam said:
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 said:
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 .
 
Joined
Nov 23, 2007
Messages
1
Reaction score
0
Old Wolf comments - is rubbish.
To finish this thema I'd like to add whole WORKING!!! realized code:
#include "stdafx.h"
#include<string.h>
#include<malloc.h>
#include <stdlib.h>
#include<stdio.h>
#include <iostream>
using namespace std;
#define SP ' '

char ** words(char *ch)// to tokenize the string based on space
{int count = 0;
int sz= sizeof(ch);
char **c= (char **)malloc(sz);
char *lst= ch;
for (int i =0; i<=sz; i++)
{c[count++] = lst;
lst = strchr(lst,SP);
*lst = '\0';
lst++;
}
return c;
};

int _tmain(int argc, _TCHAR* argv[])
{char s[] = "hello how do you do ";
char **t = words(s);
printf("the str is %s\n",t[0]);
printf("the str is %s\n",t[1]);
printf("the str is %s\n",t[2]);
printf("the str is %s\n",t[3]);
printf("the str is %s\n",t[4]);
char c1h;
cin>>c1h;
return 0;
};

Regards
Serge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top