array of structures

This is a discussion on array of structures within the C Programming forums, part of the General Programming Boards category; Hi, I need to pass an array of structures to a function, fill in the values in the structure(which will ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Unhappy array of structures

    Hi,

    I need to pass an array of structures to a function, fill in the values in the structure(which will be read from a file). Depending on the number of structures filled i have to return that number and the array. I am not getting how to pass the array and return the array and the number of structures filled.

    tish.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by tish View Post
    Hi,

    I need to pass an array of structures to a function, fill in the values in the structure(which will be read from a file). Depending on the number of structures filled i have to return that number and the array. I am not getting how to pass the array and return the array and the number of structures filled.

    tish.
    just like any other array


    Code:
    struct test 
    {
       int x;
    }
    #define MAX_STRC 10
    int main(void)
    {
       strcut test arr[MAX_STRC] = {0};
       int size = fillArr(arr,MAX_STRC);
       return 0;
    }
    
    int fillArr(struct test* pArr, int maxSize)
    {
       pArr[0].x = 1;
       pArr[1].x = 200;
       return 2;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    is it mandatory to supply the max size of the array?
    The logic i have applied is:

    In a while loop i read a string from a file, and parse it, extract some fields and assign them to the structure as:
    strcpy(pArr[i].name,tish);

    when a ',' is encountered i increment 'i' and jump back to while.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No, but if you don't supply an initial size to the array, then you'll have to malloc the size, at a later time. Which is not a big deal.

    Somehow, someway, you need to come up with an initial size for the array. It can be increased in size later, if you like (using realloc() ).
    Last edited by Adak; 04-05-2009 at 02:24 AM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    ohkai..

    somehow when i am filling in the structures in the array, i am able to print only the last structure's entries...

    What could be the reason for that?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by tish View Post
    ohkai..

    somehow when i am filling in the structures in the array, i am able to print only the last structure's entries...

    What could be the reason for that?
    Show the code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    i guess i had not incremented the pArr. It working now...

    Thanks a lot for the help...

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    i have another question:

    I need to pass another arguement to the function which would be an output parameter. The parameter would return the number of enteries of the array that have been filled.

    How can pass an output parameter as a function arguement?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by tish View Post
    i have another question:

    I need to pass another arguement to the function which would be an output parameter. The parameter would return the number of enteries of the array that have been filled.

    How can pass an output parameter as a function arguement?
    pass a pointer to the variable you want to modify
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can return the value:

    Code:
    MaxNumber = myFunction(array //whatever else);
    
    int myFunction (//your parameters here)  {
       int  i = 0;
    
       //your code that loads the array and counts the array items, here
        
       return i - 1;
    }
    If you stop incrementing i when it still has a valid data item (like i equals the very last subscript of the array), then return i. Otherwise, return i - 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  2. filling an array of structures?
    By voodoo3182 in forum C Programming
    Replies: 9
    Last Post: 08-06-2005, 05:29 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 09:55 AM
  4. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  5. Need serious help on array of structures
    By cwd in forum C Programming
    Replies: 2
    Last Post: 11-11-2001, 02:39 PM

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