![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 2
Rep Power: 0
![]() |
using `char' return type in functions
perlwhore new to C...
My 'C for Linux Programming' book says that 'char' is a valid return type for functions, but I can't get my function to return a non-int value. I searched the boards, and googled, but i can find no example. If return can't return a non-int, what's the point of using char as the function return type? I get the following error: In function `my_func': warning: return makes integer from pointer without a cast here's my code: #include <stdio.h>
char my_func(char s[]);
int main(){
char func_said;
char string[] = "foo";
func_said = my_func(string);
return 0;
}
char my_func(char s[]) {
return s;
}sorry if i'm lame. tia. |
|
|
|
|
|
#2 | |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
you are probably wanting is something along the lines of: return s[0]; To return the first char in "foo". example: #include <stdio.h>
char my_func1(char s[]);
char* my_func2(char s[]);
int main(){
char* func_says;
char func_said;
char string[] = "foo";
func_said = my_func1(string);
func_says = my_func2(string);
return 0;
}
char my_func1(char s[]) {
return s[0];
}
char* my_func2(char s[]) {
return s;
}You can basically return any little thing you fancy, as long as you declare it properly, and dont' lose it due to it being local (which is allowed, but will usually end in an exception).
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Jul 22nd, 2005 at 11:34 AM. |
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Actually, you're telling it to return a char *, which isn't the same thing as an array of char, which might be assumed from your notation (Stevens didn't make that assumption, just mentioning it). The compiler often treats them in the same way, but there be vinegar in that wine bottle. See the section, CONFUSTOIDS, in the pointers material referenced in my signature.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
yeah, between char[], char*, char* [], char **, std::string, etc, I get pretty darned confusticated myself! (prolly shows)
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Quote:
![]()
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would presume that's someone who stands around on the beach (sans spell checker) in a slinky outfit, waiting for the boats to come back from the oyster beds
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jul 2005
Posts: 2
Rep Power: 0
![]() |
stevengs,
danke! that damn asterisk... DaWei, i'm afraid you and stevengs way confusticated me with your C reparte, but thx 4 chiming in. skuinders, seesnob. ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|