new to "C" return question

This is a discussion on new to "C" return question within the C Programming forums, part of the General Programming Boards category; You can return x, or you can return y, but not both (and certainly not as a float). Also note ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    You can't return multiple values that way from a function. Instead, you need to pass a pointer to the variables:
    Code:
    #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
    Posts
    13
    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?

    edit:

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

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

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,432
    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.

Page 2 of 2 FirstFirst 12
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, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21