Get a variable out of a function.

This is a discussion on Get a variable out of a function. within the C Programming forums, part of the General Programming Boards category; Hi, I was wondering how I could get a variable out of a function. For example, in the function scanf, ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Get a variable out of a function.

    Hi, I was wondering how I could get a variable out of a function.
    For example, in the function scanf, if you do
    Code:
    scanf("%d", &var);
    The variable var has the value of whatever you put in, and can be used.

    Here are a few snippets of my code:
    Code:
    int getValue (int *arr, int numElements, int pos, int value) {
        int c, o;
        if (pos > numElements) {
            c = 2;
        } else if (arr[pos] < 0) {
            c = 1;
        } else {
            c = 0;
            value = arr[pos]; // I need this able to be used outside of getValue
        }
        return (c);
    }
    // This is how I would need it to be used.
    if (getValue(arr, mpos, n, o) == 0) {
            printf("array[%d] is %d\n", n, o);
            exit(0);
    }
    I'm not really sure what to do. I tried doing value* = arr[pos]; but I get the error "error: invalid type argument of ‘unary *’". Please help (:

    UPDATE: Fixed.
    Last edited by dinostatic; 02-08-2012 at 10:54 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    You should then make value into a pointer to an int so that it can be used as an out parameter, e.g.,
    Code:
    *value = arr[pos];
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Sorry, what I wrote was a typo. When I do
    Code:
    *value = arr[pos];
    it gives me the error: invalid type of 'unary *', do you know why that is?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    I just got it to work. You're right it needed to be *value = arr[pos]; and I needed to change the type of value in the function to int *value. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function with variable function arguments
    By Kyl in forum C Programming
    Replies: 10
    Last Post: 12-21-2011, 01:56 PM
  2. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  3. Replies: 8
    Last Post: 02-14-2010, 03:14 PM
  4. Variable Function?
    By SirCrono6 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 02:04 AM
  5. more than one function variable?
    By lwong in forum Windows Programming
    Replies: 9
    Last Post: 11-17-2003, 07:29 PM

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