C++ - dynamically named structure instances

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • priyachari
    New Member
    • Jan 2007
    • 11

    C++ - dynamically named structure instances

    I have a structure, say
    typedef struct
    {
    char a[10];
    char b[10];
    int i;
    } test;

    Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
    For ex., if user says 3 which has to be prefixed with "US", i need to create

    test *UStesting1;
    test *UStesting2;
    test *UStesting3;

    and i need to load various file into each instance.
    For e.g, USTesting1 will contain data from File US1.in;
    USTesting2 will contain data from File US2.in;
    & USTesting2 will contain data from File US3.in;

    Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

    How can i do this?

    Can anyone help me on this, since this is a bit urgent!!

    Thanks in advance,
    Priya
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by priyachari
    I have a structure, say
    typedef struct
    {
    char a[10];
    char b[10];
    int i;
    } test;

    Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
    For ex., if user says 3 which has to be prefixed with "US", i need to create

    test *UStesting1;
    test *UStesting2;
    test *UStesting3;

    and i need to load various file into each instance.
    For e.g, USTesting1 will contain data from File US1.in;
    USTesting2 will contain data from File US2.in;
    & USTesting2 will contain data from File US3.in;

    Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

    How can i do this?

    Can anyone help me on this, since this is a bit urgent!!

    Thanks in advance,
    Priya
    U will need to run a programm that will edit code of programm which have class instances then compile it in command line and run it.

    Now,which compiler do u use?

    Savage

    Comment

    • Darryl
      New Member
      • May 2007
      • 86

      #3
      You can't dynamically create variable names. I believe you have mis-interpreted your assignment.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by priyachari
        I have a structure, say
        typedef struct
        {
        char a[10];
        char b[10];
        int i;
        } test;

        Based on the user input of how many instances to create , i should create instances prefixed with runtime data.
        For ex., if user says 3 which has to be prefixed with "US", i need to create

        test *UStesting1;
        test *UStesting2;
        test *UStesting3;

        and i need to load various file into each instance.
        For e.g, USTesting1 will contain data from File US1.in;
        USTesting2 will contain data from File US2.in;
        & USTesting2 will contain data from File US3.in;

        Again, i'm using malloc and free to decide the size of the array structure based on the number of entries in input file.

        How can i do this?
        I would create a containing structure

        [code=c]
        typedef struct
        {
        char prefix[10];
        int count;
        test *array;
        } test_container;
        [/code]

        Then you can fill in prefix and count with input from the user. Use count to malloc an array of test structures and store in the array member. Then you can iterate through each of these structures open the file and fill in the structure.

        Pseudo coded as

        [code=c]
        test_container container;

        container.prefi x = GetPrefixFromUs er();

        container.count = GetCoutFromUser ();

        container.array = (test *)malloc(sizeof *container.arra y * continer.count) ;

        for ix = 0 to count -1
        {
        filename = continer.prefix + (ix+1) + ".in"

        Load continer.array[ix] from filename
        }
        [/code]

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Going back to the original problem, if the user says 3 you need to have:

          [code=c]
          test *UStesting1;
          test *UStesting2;
          test *UStesting3;
          [/code]

          So, you malloc() and array of 3 test:

          [code=c]
          test* arr = (test*)malloc(3 * sizeof(test));
          [/code]

          Yes?

          So you use arr[0] instead of UStesting1, arr[1] instead of UStesting2 and arr[2 ] instead of UStesting3.

          You cannot create variable names at run time but you can allocate an array whose size won't be known until run time and if you do that, the array elements can be used as variable names.

          Comment

          Working...