CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    23

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    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.

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: returning a char as a pointer

    Quote Originally Posted by Vanilla_Rice View Post
    Code:
    void position (char, char *);
    
    int main (void);
    {
         char orientation;
         char orien;
         position(orientation, *orien);
    
    //...
    }
    This doesn't compile. Fix the compiler errors first.
    Quote Originally Posted by Tronfi View Post
    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.
    Code:
    char* orien = 0;
    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

  4. #4
    Join Date
    May 2002
    Posts
    1,435

    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;
    }

  5. #5
    Join Date
    Mar 2009
    Posts
    23

    Re: returning a char as a pointer

    thanks for the reply's, I have it working now.

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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
  •  





Click Here to Expand Forum to Full Width

Featured