Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning char** from a function.

Reply
Thread Tools

Returning char** from a function.

 
 
noridotjabi@gmail.com
Guest
Posts: n/a
 
      07-31-2006
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?

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-31-2006
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> 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.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      07-31-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>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 <stdio.h> for the prototype of printf()

> 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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-31-2006
(E-Mail Removed) wrote:
> 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.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-01-2006
(E-Mail Removed) wrote:
> [...]
> 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?"

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      08-01-2006
(E-Mail Removed) wrote:

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

--
Chris "where does a flame go when it's blown out?" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
cannot access Internet after returning from standby =?Utf-8?B?REJI?= Wireless Networking 0 10-31-2005 09:21 PM
Returning multiple variables Kwaj VHDL 2 06-07-2004 10:20 AM
WebService – Returning an Object Karuppasamy ASP .Net 0 01-15-2004 04:38 PM
Re: Net::SNMP - returning huge hex for opaque value dna Perl 0 10-20-2003 02:41 PM



Advertisments