Thread: How do I send pointers into this function?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    How do I send pointers into this function?

    I have the following function declaration:
    Code:
    extern int function(int *grade, double *average);
    How do I send numbers into this function?


    I tried:
    Code:
    int *grade;
    double *average;
    function(*grade, *average);
    I also tried it without the *. Both give me errors..

    ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    function(grade, average) should be correct, except that grade and average are not initialised. What exactly does this function do? Perhaps grade and average should not be pointers, and instead you want to call function(&grade, &average).
    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 C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    int grade, average;
    function(&grade, &average);
    the & means the address

    EDIT: Just show lasersights post. You probably want to do what I posted. It is not wrong what you are doing, but I guess the function expects to pass a pointer that actually points at an allocated space.
    Try what I posted and try this also:
    Code:
    int *grade = malloc(sizeof(int));
    int *average = malloc(sizeof(int));
    function(grade, average);
    Last edited by C_ntua; 10-15-2008 at 11:40 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by C_ntua View Post
    Code:
    int grade, average;
    function(&grade, &average);
    the & means the address

    EDIT: Just show lasersights post. You probably want to do what I posted. It is not wrong what you are doing, but I guess the function expects to pass a pointer that actually points at an allocated space.
    Try what I posted and try this also:
    Code:
    int *grade = malloc(sizeof(int));
    int *average = malloc(sizeof(int));
    function(grade, average);
    PERFECT.. thanks

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    scarlet00014, what sort of book are you using to learn C?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM