Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 22nd, 2005, 10:42 AM   #1
atreyu
Newbie
 
Join Date: Jul 2005
Posts: 2
Rep Power: 0 atreyu is on a distinguished road
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.
atreyu is offline   Reply With Quote
Old Jul 22nd, 2005, 11:14 AM   #2
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by atreyu
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.
well, your function wants to return a single char, but you are telling it to return s, which is char []. There is a big difference between the two. What
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.
stevengs is offline   Reply With Quote
Old Jul 22nd, 2005, 11:20 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 22nd, 2005, 11:31 AM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
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
stevengs is offline   Reply With Quote
Old Jul 22nd, 2005, 11:43 AM   #5
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Quote:
Originally Posted by atreyu
perlwhore...
lol wtf?? Sorry, I stopped reading right there.
__________________
"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
skuinders is offline   Reply With Quote
Old Jul 22nd, 2005, 12:10 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 22nd, 2005, 12:13 PM   #7
atreyu
Newbie
 
Join Date: Jul 2005
Posts: 2
Rep Power: 0 atreyu is on a distinguished road
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.
atreyu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC