Another struct questions

This is a discussion on Another struct questions within the C Programming forums, part of the General Programming Boards category; I need to create an unknown number of structs in my application. Does anybody have any recommendations on doing this? ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    Another struct questions

    I need to create an unknown number of structs in my application. Does anybody have any recommendations on doing this? I can easily do this inside a function, but then the struct I create inside that function is only available within that function

    Code:
    structs.h
    struct a
    {
      int a;
      int b;
    };

    Code:
    main.c
    #include "structs.h"
    void funcCreateNewStruct (int i)
    {
      struct a New[i];
      ...initialise etc....
    }
    
    main
    {
    int i = 9;
    funcCreateNewStruct(i);
    
    ...do something with new struct...
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    You pass it as an argument to another function.
    The function simply needs to take a pointer as argument:

    void foo(mystruct* s)
    Or
    void foo(mystrcut s[])

    Both do the same thing, but with different syntax. Remember that when passing an array, you automatically pass a pointer to the first element, so you shouldn't take the address of it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    To create an arbitrary number of structs, you use dynamic memory allocation (ie. malloc()).

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Is this what prelude is saying here

    Code:
    struct Node *make_node ( void )
    {
      struct Node new_node = malloc ( sizeof *new_node );
    
      /* Error check and set a default state */
    
      return new_node;
    }
    
    or
    
    int make_node ( struct Node **new_node )
    {
      int rc = EXIT_SUCCESS;
    
      new_node = malloc ( sizeof *new_node );
      /* Error check and set a default state */
    
      return rc;
    }
    Or is it possible to do this
    Code:
    struct Person {
      char *name;
      int age;
      float height;
    };
    
    struct Person people[] = {
      {"Prelude", 25, 5.9f},
      {"Generic person", 20, 6.0f}
    };
    and then add another person later?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Both work, except the former is for linked lists. The latter is easier if you don't have to alter the data and don't have to add or delete anymore structs to the array.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    Code:
    int make_node ( struct Node **new_node )
    {
      int rc = EXIT_SUCCESS;
      *new_node = malloc ( sizeof(**new_node) );
      /* Error check and set a default state */
      return rc;
    }
    Correction in code.

    It is not possible to add more stuff at a later time with the last snippet you made, because it's static, as opposed to the first snippets which are dynamic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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