Re: Returning an array of strings in C



karthika.28@xxxxxxxxx wrote:
# Hi,
#
# I am writing a function that needs to return an array of strings and I
# am having some trouble getting it right. Can you help me answer the
# following questions?
#
# 1. How does the function return the array?

It doesn't. It returns a pointer. You have to ensure the memory
is available to the caller after the return; usually that means
static or mallocked from the heap. If you return mallocked
memory, the caller has to free to reclaim the heap.

# 2. How should the function be declared?

char *dupstr(char *string) {
return strcpy(malloc(strlen(string)+1),string);
}

# 3. How is the return value captured by the calling program?

You can assign it to appropriate pointer variable.

char *duplicate = dupstr("aleph beth gimel");

# Here is what I consider an array of 100 strings:
# char *array_string[100]

You might have to do something like
char **function(void) {
char **result = malloc(100*sizeof(char*));
int i;
for (i=0; i<100; i++) {
result[i] = malloc(n(i));
}
return result;
}

...
char **A = function(); int i;
....
for (i=0; i<100; i++) free(A[i]); free(A);
...

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Death is the worry of the living. The dead, like myself,
only worry about decay and necrophiliacs.
.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: Returning array of strings through callback from unmanaged to
    ... Is the array of strings a jagged array or fixed length array? ... It is imperative that I am able to return the array of strings from the ... I have control over how many strings I want the unmanaged code to return ... What I want to accomplish is to pass a callback function to unmanaged ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
    (comp.lang.php)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)