|
-
May 24th, 2009, 05:44 AM
#1
returning a char as a pointer
Just after a little help here, I've shrunk the code down to keep it simpile, and I'm hoping there is an obvious reason why it wont return the character that I enter, any thoughts?? The same way seems to work for int and doubles but not for char.
Code:
void position (char, char *);
int main (void);
{
char orientation;
char orien;
position(orientation, *orien);
printf("Orientation is: %c", orien);
fflush(stdin);
getc(stdin);
return 0;
}
void position (char orientation, char *orien)
{
scanf("Selection is: %c", &orientation);
*orien = orientation;
}
Last edited by Vanilla_Rice; May 24th, 2009 at 05:50 AM.
-
May 24th, 2009, 06:05 AM
#2
Re: returning a char as a pointer
Try this:
Code:
void position (char, char *);
int main (void);
{
char orientation;
char *orien;
position(orientation, orien);
printf("Orientation is: %c", *orien);
fflush(stdin);
getc(stdin);
return 0;
}
void position (char orientation, char *orien)
{
scanf("Selection is: %c", &orientation);
*orien = orientation;
}
Last edited by Tronfi; May 24th, 2009 at 06:08 AM.
-
May 24th, 2009, 06:26 AM
#3
Re: returning a char as a pointer
 Originally Posted by Vanilla_Rice
Code:
void position (char, char *);
int main (void);
{
char orientation;
char orien;
position(orientation, *orien);
//...
}
This doesn't compile. Fix the compiler errors first.
 Originally Posted by Tronfi
Try this:
Your code has undefined behaviour. You are dereferencing an uninitialised pointer. It's a good custom to initialise any pointer you define with 0 or a valid address.
Then when you dereference a pointer, check that it's not 0.
Code:
void position (char orientation, char *orien)
{
if (!orien) {
throw std::invalid_argument("NULL pointer");
}
scanf("Selection is: %c", &orientation);
*orien = orientation;
}
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 24th, 2009, 06:43 AM
#4
Re: returning a char as a pointer
Code:
int main (void);
{
char orientation;
char orien;
position(orientation, &orien);
printf("Orientation is: %c", orien);
fflush(stdin);
getc(stdin);
return 0;
}
void position (char orientation, char *orien)
{
scanf("Selection is: %c", &orientation);
*orien = orientation;
}
-
May 24th, 2009, 09:57 PM
#5
Re: returning a char as a pointer
thanks for the reply's, I have it working now.
-
May 25th, 2009, 02:56 AM
#6
Re: returning a char as a pointer
That code looks quite weird to me. None of the two parameter are really needed:
1) The first parameter is used to store a letter from keyboard input. There´s no need to pass it, it should be local.
2) Only the second parameter is modified and the function does not return anything. It should return the letter typed in by the user
Code:
char position()
{
char orientation;
scanf( "Selection is %c", &orientation );
return orientation;
}
int main()
{
char orientation = position();
}
Using C++ streams might even be a better solution.
- Guido
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|