Thread: confusion over use of a function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    confusion over use of a function

    Hi. I've got this project for my c programming class that has me a little confused. Here's the part that confuses me:

    "You must use the following functions (prototypes shown) to perform the actions described.

    void getDatabase(char metals[][30], double aValues[], double bValues[], double minTemp[], double maxTemp[], int *number);

    This function will prompt for the name of a file containing the database, open the file, read the data into the arrays, and close the file. The data file is a text file, the first line of which is an integer, n which is the number of following lines (Place this value in the last parameter, number)."

    My question is how do I enter the parameters for this function in the calling program when those parameters are unknown until it opens the database file? Is there some trick with pointers? I can't help but be feel I'm missing something simple. Any advice would be much appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Arrays are passed by pointer.... so the various arrays in your function call are actually pointers to the data that you can manipulate from within the function. You can declare pointers globally before calling the function and use malloc() or calloc() within the function to allocate memory.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by flyingants
    My question is how do I enter the parameters for this function in the calling program when those parameters are unknown until it opens the database file? Is there some trick with pointers? I can't help but be feel I'm missing something simple. Any advice would be much appreciated.
    I suggest that you assume that there is some maximum number of database entries, say #define MAX_ENTRIES 100. Now, you can write:
    Code:
    char metals[MAX_ENTRIES][30];
    double aValues[MAX_ENTRIES];
    /* etc */
    And pass these to the function. The caller would then use the last argument to find out how many entries there really are.

    Of course, there would be a problem if the actual number of entries is greater than the maximum that you assumed. Normally, we might use CommonTater's suggestion in post #2 to get around this, but unfortunately you cannot actually make use of CommonTater's suggestion given your function prototype since you need the caller to pass a pointer to a pointer for this to work. The best you can do now is to just stop reading and ignore whatever entries there are beyond your assumed maximum.
    Last edited by laserlight; 11-12-2010 at 08:12 AM.
    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

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Code:
    // In main():
    // (1) Define variables...
    char    (*metals)[30];
    double  *aValues;
    double  *bBalues;
    double  *minTemp;
    double  *maxTemp;
    int     records;
    
    // (2) Call your function...
    getDatabase(metals, aValues, bValues, minTemp, maxTemp, &number);
    
    
    // Definition of getDatabase().
    void getDatabase(char metals[][30], double aValues[], double bValues[],
                     double minTemp[], double maxTemp[], int *number)
    {
        getDbName();
        openDbByName();
        *number = readDbHeader();
        allocRecords(metals, aValues, bValues, minTemp, maxTemp, number);
        readDbData(metals, aValues, bValues, minTemp, maxTemp, number);
        closeDb();
    }
    
    void allocRecords(char metals[][30], double aValues[], double bValues[],
                      double minTemp[], double maxTemp[], int *number)
    {
        // Set $metals, $aValues, $bValues, $minTemp, $maxTemp to point to
        // appropriately sized (based on value of $number) malloc()'ed memory.
    }
    Main point is that type-name variable-name[] is the same as type-name *variable-name.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by msh
    Main point is that type-name variable-name[] is the same as type-name *variable-name.
    ... where it appears in a parameter list.

    Unfortunately, msh's suggestion is effectively the same as CommonTater's suggestion. With your given function prototype, it cannot work. (Well, if you take CommonTater's "declare pointers globally" suggestion literally then it can work, but you'll basically be ignoring the parameters in favour of global variables, which should generally be avoided. If I were your instructor, you'll be lucky to barely pass this assignment with such code.)
    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

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Could you please explain the issue? I'm missing something here.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by msh
    Could you please explain the issue? I'm missing something here.
    What you are suggesting is similiar to this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void foo(int *p)
    {
        p = malloc(sizeof(*p));
        *p = 123;
    }
    
    int main(void)
    {
        int *p = 0;
        foo(p);
        printf("%d\n", *p);
        free(p);
        return 0;
    }
    The problem is that p in the main function has not been changed. After the call to foo(p), it is still a null pointer. The return value of malloc in foo has been assigned to the parameter p, which is local to foo. To it properly, you could change it to:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void foo(int **p)
    {
        *p = malloc(sizeof(**p));
        **p = 123;
    }
    
    int main(void)
    {
        int *p = 0;
        foo(&p);
        printf("%d\n", *p);
        free(p);
        return 0;
    }
    But flyingants has no such liberty as he/she is required to use the given function prototype.
    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

  8. #8
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Ah! Now I understand. Thanks!

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    I figured it out. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM