Thread: Passing structure arrays to function using pointers

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    Passing structure arrays to function using pointers

    Hello. I'm having a bit of trouble with an assignment where I'm supposed to pass arrays of structures to a function using a pointer.

    This is where I'm at right now:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define itemNum 4
    #define INPUT1 "INPUT1.txt"
    #define INPUT2 "INPUT2.txt"
    
    
    struct item
    {
        int itemCode;
        char item[20];
        int median;
    };
    
    
    struct storage
    {
        int itemCode;
        int storageState;
    };
    
    
    void readFile(struct item *pStr[itemNum], struct storage *tStr[itemNum]);
    
    
    
    int main(void)
    {
        struct tooted p[tooteArv];
        struct laoseis t[tooteArv];
        readFile(&p, &t);
            <more stuff here>
            return 0;
    }
    
    
    void readFile(struct item *pStr[itemNum], struct storage *tStr[itemNum])
    {
        int i;
        FILE *fi1, *fi2;
        fi1 = fopen(INPUT1, "r");
        if(!fi1)
        {
            printf("INPUT FILE ERROR\n");
            exit(1);
        }
        for(i = 0; i < itemNum; i++)
        {
            fscanf(fi1, "%d", &pStr[i]->itemCode);
            fscanf(fi1, "%s", pStr[i]->item);
            fscanf(fi1, "%d", &pStr[i]->median);
        }
        fclose(fi1);
        fi2 = fopen(INPUT2, "r");
        if(!fi2)
        {
            printf("INPUT FILE ERROR\n");
            exit(1);
        }
        for(i = 0; i < itemNum; i++)
        {
            fscanf(fi2, "%d", &tStr[i]->itemCode);
            fscanf(fi2, "%d", &tStr[i]->storageState);
        }
        fclose(fi2);
    }
    I'm aware this might look completely wrong at current time, since the pointers were not applied in the main function, but I couldn't figure out how to apply them to a structure array.

    I'm looking for help on how to correctly point to a structure array, pass it into a function via the pointer and apply values to the corresponding structure members from within the function.
    Thanks!

    Small mistakes may be included, for the variables were translated from another language than the chunk was originally written in.

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    2
    I'm sure I also made a mistake in trying to make a pointer into an array, but I just got to working on structures and pointers and right now I have a lot more questions than answers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall how you might declare an array of integers and pass it to a function:
    Code:
    /* ... */
    
    #define NUM_NUMBERS 20
    
    size_t readNumbers(int numbers[], size_t max_size);
    
    int main(void)
    {
        int numbers[NUM_NUMBERS];
        size_t size = readNumbers(numbers, NUM_NUMBERS);
        size_t i;
        for (i = 0; i < size; ++i)
        {
            printf("%d\n", numbers[i]);
        }
        return 0;
    }
    
    /* ... */
    Now, suppose you have an array of struct objects instead. The syntax is similiar:
    Code:
    /* ... */
    
    struct item
    {
        int itemCode;
        char item[20];
        int median;
    };
    
    #define NUM_ITEMS 20
    
    size_t readItems(struct item items[], size_t max_size);
    
    int main(void)
    {
        struct item items[NUM_ITEMS];
        size_t size = readItems(items, NUM_ITEMS);
        size_t i;
        for (i = 0; i < size; ++i)
        {
            printf("%d\n", items[i].itemCode);
        }
        return 0;
    }
    
    /* ... */
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM
  5. Passing Structure Pointers
    By mattz in forum C Programming
    Replies: 12
    Last Post: 11-18-2001, 06:23 AM

Tags for this Thread