#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    returning array of structure from a function


    Hi, i hope someone out there can help me with this C problem:

    i have a function called loaddata(). that function collects data and stores them in a array of structures. How do i make the function return this "type"(students) and how do i call the function? (is it possible at all??)

    this is the structure:

    struct student
    {
    char studentid[9];
    char sex[1];
    char age[10];
    char school[40];
    };

    and this is the array:

    struct student students[100];

    Thanks for your help!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Yes, what you want to do is very common. The C solution is to pass a pointer to the function. In the case of a single struct, you would have to explicitly pass a pointer. In the case of an array, since the array name is already equivalent to a pointer you would just pass the name.

    For example:
    Code:
    struct student 
    { 
    char studentid[9]; 
    char sex[1]; 
    char age[10]; 
    char school[40]; 
    }; 
    
    and this is the array: 
    
    struct student students[100]; 
    
    void ProcessSingleStruct(struct student *estudiante);
    
    /* the following declarations are equivalent; they both mean the same thing */
    void ProcessArrayOfStructs(struct student ary[]);
    void ProcessAnotherArray(struct student *ary);
    
    int main(void)
    {
        /* process a single student record */
        ProcessSingleStruct(&students[4]);
    
        /* Process the array */
        /*  note that since the array name is a pointer, 
                you do not need to use the address operator (&) */
        ProcessArrayOfStructs(students);
        ProcessAnotherArray(students);
    
        return 0;
    }
    
    void ProcessSingleStruct(struct student *estudiante)
    {
        strcpy(estudiante->studentid,"123456789"); 
        estudiante->sex = 'M'; 
        strcpy(estudiante->age,"18"); 
        strcpy(estudiante->school,"Riverdale High"); 
    }
    
    void ProcessArrayOfStructs(struct student ary[])
    {
        strcpy(ary[4].studentid,"123456789"); 
        ary[4].sex = 'M'; 
        strcpy(ary[4].age,"18"); 
        strcpy(ary[4].school,"Riverdale High"); 
    }
    
    
    /* the function body is identical, because you access the
        elements of ary the same regardless of which way you
        declared it. */
    void ProcessAnotherArray(struct student *ary)
    {
        strcpy(ary[4].studentid,"123456789"); 
        ary[4].sex = 'M'; 
        strcpy(ary[4].age,"18"); 
        strcpy(ary[4].school,"Riverdale High"); 
    }
    Please note that I ignored the obvious solution of making the array global (which it already is in this example). To do so would not have allowed us to explore the other options.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0
    Thanks for that, i think i got it to work.

    is it correct, that:

    I create the structure in the main function and then pass it as a pointer into my "loaddata" function? and then i can use it in the main function when the data is loaded?

    I have another quick question: How do i know the size of the array(of structure). I tried to use sizeof(students), but that returns 4 even though there only is 2 in the array..

    Thanks for your help, i really appreciate it!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    is it correct, that: I create the structure in the main function and then pass it as a pointer into my "loaddata" function? and then i can use it in the main function when the data is loaded?

    Yes, that's correct. One minor point: you are not passing the "structure as a pointer" to the function, instead you are "passing a pointer" to the structure. The structure is located at a spot in memory, and a pointer contains the address of that location, and that address is what you are passing to the function.

    then i can use it in the main function when the data is loaded?

    Yes, the pointer you pass to the function is used to change the structure. As long as you have a pointer in main() that points to the struct, you will be able to access the struct and its changes.

    I have another quick question: How do i know the size of the array(of structure). I tried to use sizeof(students), but that returns 4 even though there only is 2 in the array..

    sizeof(students) returns the total number of bytes of memory allocated for the array when you declared it. sizeof(students[0]) will return the number of bytes occupied by the first element of the array. So, if you want to know how many elements will fit in the array, you use sizeof(students)/sizeof(students[0]).
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0
    Thanks for that reply.

    Regarding the sizeof question:

    I want to do a loop of all the students. Eg. like this:

    for(i=0; i < sizeof(students);i++) {

    printf("Student: %i\n", students[i].age);

    }

    But i don't think sizeof is the right one to use here...

    I hope it makes sence.. ;)


    Thanks!!!!!
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    If you want to know how many elements are in the array, you either have to pass an integer value with the number or store some known invalid value in the last slot of the array (that is how character strings work, they NULL terminate them). sizeof only tells you how many bytes are in a structure that the compiler knows about as it compiles.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo