Returning char** from a function.

N

noridotjabi

Why is it not possible to return char** from a funcion. For example:

char** foo(void) {
char[][] foobar = {"foo", "bar"};

return foobar;
}

int main(void) {
printf("%s\n", foo()[0]);

return 0;
}

I have no idea why this does not work. Maybe I am doing something
wrong? This is not the actual code that I am comping but I didn't want
to post that as the relevant code is 200 lines long. I get warnings
when I try to compile this and a segmentation fault upon function call.
Thanks.
Nori

P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?
 
B

Ben Pfaff

Why is it not possible to return char** from a funcion. For example:

char** foo(void) {
char[][] foobar = {"foo", "bar"};

return foobar;
}

There are at least three problems here. First, char[][] is not a
valid type. Only the first in a sequence of array declarators
may have empty brackets.

Second, if it were a valid type, char[][] would not be the same
type as char **. The name of an array used in an expression is,
in most circumstances, converted to a pointer to its first
element, which in this case would result in type "char (*)[]",
that is, a pointer to an array. A pointer to an array is not
compatible with a pointer to a pointer.
P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?

Third, you're trying to return the address of a local variable.
The correct thing to do is to not return the address of a local
variable. One way to do that would be to declare the array
"static", but I'd recommend doing that only if its value never
changes, in which case "const" might also be warranted.
Alternatively, you could dynamically allocate your array.
 
I

Ike Naar

Why is it not possible to return char** from a funcion. For example:

char** foo(void) {
char[][] foobar = {"foo", "bar"};

This is a syntax error, the '[]' should follow 'foobar'.
return foobar;

You are returning a pointer to a local variable;
foobar ceases to exist when you return from the function.

A possible solution is to make foobar static:
static char * foobar[] = {"foo", "bar"};
}

int main(void) {
printf("%s\n", foo()[0]);

You also need to #include said:
return 0;
}

I have no idea why this does not work. Maybe I am doing something
wrong? This is not the actual code that I am comping but I didn't want
to post that as the relevant code is 200 lines long. I get warnings
when I try to compile this and a segmentation fault upon function call.
P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?

Yes. Don't return the address of a local variable.

Regards,
Ike
 
A

Andrey Tarasevich

Why is it not possible to return char** from a funcion.

It _is _ possible to return a 'char**' from a function.
For example:

char** foo(void) {
char[][] foobar = {"foo", "bar"};

Invalid declaration.

Firstly, in an array declaration with an initializer only the very first pair of
square brackets can be left empty. The one that follow must specify a concrete size.

Secondly, the '[]' is supposed to follow the identifier, not the type name

char foobar[][4] = {"foo", "bar"};
return foobar;

'foobar' is a two-dimensional array of 'char', not a 'char**'. It cannot be
returned as 'char**'.
P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?

Yes. Stop doing this. Returning address of local object (an that's what you are
trying to do) is completely useless.
 
E

Eric Sosman

[...]
P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?

Easy: Don't return pointers to local variables, which will
cease to exist before the caller receives the returned pointer.

"Go to the stadium, wearing a yellow carnation in your
right lapel and carrying a copy of `Winnie the Pooh' in your
left hand, and deliver this envelope to the occupant of seat
49B in section 22."

"Sure, boss -- any particular game you had in mind?"
 
C

Chris Dollin

P.S. Is there anyway to get rid of that pesky "function returns adress
of local variable" warning?

Yes. Don't return the address of a local variable.

(The variable evaporates when you leave the function, so the address
becomes meaningless, and any use of it -- /any/ use of it -- permits
the implementation to do whatever it likes. If you're lucky, you'll
get an immediate loud program failure. If not, you're code will break
later, incomprehensibly and possibly expensively.)
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top