using a variable array of structures

This is a discussion on using a variable array of structures within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to have a variable array of a structure in a different structure and then call a function ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    114

    using a variable array of structures

    Hi I'm trying to have a variable array of a structure in a different structure and then call a function that writes to one of the structures in the array. I think I have got the right idea but I have typed out some example code of how I am doing it to make sure.

    Code:
    struct Sworld
    {
        world variables go in here...
    
        int board_count;
        struct Sboard *board;
    };
    
    struct Sboard
    {
       board variables go in here...
    };
    
    struct Sworld world;
    
    int main()
    {
        int i;
    
        world.board = malloc( sizeof(struct Sboard) * world.board_count);
    
        for (i = 0; i < world.board_count; i++)
            load_board(&world.board[i]);
    }
    
    void load_board(struct Sboard *board)
    {
        board->variable = somthing;
    }
    This look good? Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I don't see anything wrong with your logic. Obviously there are problems with your posted code, but I'm assuming it was meant as an outline rather than something you plan on compiling

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Looks good kzar
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    114
    cracking, thought so

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    114
    hey I tried to free the board array but it crashes it?
    Code:
    if (world.board != NULL)
        free(world.board);

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Look at your other use of malloc and free.
    Make sure
    - you're not running off the end of the memory you allocate
    - you're not freeing the same thing twice
    - you're not freeing things you didn't malloc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    114
    Quote Originally Posted by Salem
    Look at your other use of malloc and free.
    Make sure
    - you're not running off the end of the memory you allocate
    - you're not freeing the same thing twice
    - you're not freeing things you didn't malloc
    ok. Am I right in saying that before a pointer is malloc'd its NULL and after its freed its set to NULL automatically?

  8. #8
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Quote Originally Posted by kzar
    ok. Am I right in saying that before a pointer is malloc'd its NULL and after its freed its set to NULL automatically?
    No, unless you haven't initialized it point to NULL.
    uninitialized pointer value can contain anything in it's value context and
    initialized pointer has its value as long it's value is changed to point
    somewhere else.
    -- Add Your Signature Here --

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. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM

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