Thread: new to "C" return question

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You can return x, or you can return y, but not both (and certainly not as a float). Also note that just because the letters are the same does not mean that x and y in input have any relation to x and y in main.

  2. #17
    Join Date
    Dec 2007
    You can't return multiple values that way from a function. Instead, you need to pass a pointer to the variables:
    #include <stdio.h>
    // Receive pointers to the variables
    void input(int *x, float *y)
       printf("Enter an integer: ");
       scanf("%d", x);
       printf("Enter a real number: ");
       scanf("%f", y);
    int main(void)
       int x = 0;
       float y = 0.0f;
       // Pass the addresses of the variables
       // to the function.
       input(&x, &y);
       // Print the values of the variables
       printf("Got %d and %f\n", x, y);
       return 0;

  3. #18
    Registered User
    Join Date
    Sep 2008
    I've heard of pointers but haven't learned about them yet. If I understand your code though the main difference seems to be the asterisk in front of x and y when you "declare" (is that even what it's called) them in input(). also you set initial values in main() and when you call the function you use & instead of the variable type. am I missing any key data about these and are my assumption correct?

    thanks for the help by the way?


    also you just wrote the function. would the prototype look like this

    void input(int *x, int *y);
    or like this
    void input(int x, int y);
    Last edited by demuro1; 09-26-2008 at 07:05 PM.

  4. #19
    Join Date
    Oct 2007
    Inside my computer
    Yes, pointers mean you put a * before the type, between the type or beside the name:
    int* x
    int * x
    int *x
    All the same thing.
    And since pointers hold the address of something, you do indeed need to pass the variables using &, to pass their address.

    Perhaps it would be best to study pointers a little before you start using them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  4. Class Bank(i have a question)
    By kuwait in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 07:40 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM