Returning an array of strings in C

K

karthika.28

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?
2. How should the function be declared?
3. How is the return value captured by the calling program?

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

Thanks,
-Karthika
 
S

SM Ryan

(e-mail address removed) 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 = malloc(n(i));
}
return result;
}

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

wcang79

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

char ** strings()
{
int i = 0;
char ** strings;

strings = (char **) malloc(sizeof(char *) * 100);

for (i = 0; i < 100; i++) {
strings = malloc(4);
sprintf(strings, "%d", i);
}

return strings;
}


int main()
{
int i;
char ** str;

str = strings();
for (i = 0; i < 100; i++) {
printf("%s ", str);
free(str);
}
free(str);
printf("\n");

return 0;
}

Hope this help. I don't know how to handle returning char *
array_strings[100] cause that will return local address to another
function and will cause segfault. I'm just a lousy C programmer.
 
P

pete

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
strings = (char **) malloc(sizeof(char *) * 100);

strings = malloc(100 * sizeof *strings);

The above way is generally considered a
better style way of writing that.

strings = malloc(4);


strings = malloc(3 + 1);

This makes it more obvious that string
is intended to be holding a string,
instead of pointing to a string.
 
P

pete

pete said:
(e-mail address removed) wrote:
strings = malloc(4);


strings = malloc(3 + 1);

This makes it more obvious that string
is intended to be holding a string,
instead of pointing to a string.


That doesn't make as much sense to me
as I thought it did when I wrote it.

.... more obvious that string
is intended to be pointing a string,
instead of pointing to a pointer.

?

I don't know.
 
K

Keith Thompson

pete said:
pete said:
strings = malloc(4);


strings = malloc(3 + 1);

This makes it more obvious that string
is intended to be holding a string,
instead of pointing to a string.


That doesn't make as much sense to me
as I thought it did when I wrote it.

... more obvious that string
is intended to be pointing a string,
instead of pointing to a pointer.

?

I don't know.


The explicit "+ 1" makes it obvious that you're allocating an extra
byte to hold the terminating '\0'. But ordinarily you shouldn't use
magic numbers, either 3 or 4. The original context was:

for (i = 0; i < 100; i++) {
strings = malloc(4);
sprintf(strings, "%d", i);
}

The maximum length of the stored strings is actually 2 (the largest
number passed to sprintf is 99, which has 2 digits); I don't know why
4 bytes are allocated. It would be better to declare a constant.

BTW, malloc(4) implies a pointer allocation only if you assume that
the author assumes that pointers are 4 bytes.
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top