473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Returning char** from a function.

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?

Jul 31 '06 #1
5 3075
"no*********@gmail.com" <no*********@gmail.comwrites:
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
Jul 31 '06 #2
In article <11*********************@s13g2000cwa.googlegroups. com>,
no*********@gmail.com <no*********@gmail.comwrote:
>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.hfor 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
Jul 31 '06 #3
or********@gmail.com 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
Jul 31 '06 #4
no*********@gmail.com 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
es*****@acm-dot-org.invalid
Aug 1 '06 #5
no*********@gmail.com 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/

Aug 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
7
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
4
by: Erich Neuwirth | last post by:
I am calling a function in a DLL from VB6, and I need to convert the code to VB.NET. The function in the DLL is written in C and returns a char * By googling around I found that the way to handle...
5
by: shyam | last post by:
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 *) ...
11
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.