populate a fixed sized array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaffekopp
    New Member
    • Feb 2008
    • 2

    populate a fixed sized array

    Hi,
    Since this is the first time for me using fixed size arrays, i would appreciate some guidance to solve this question.

    First of all, these structures are to be used:

    struct song
    {
    char *name;
    unsigned int minutes;
    unsigned int seconds;
    };

    struct idtag
    {
    struct song *song;
    unsigned int year;
    unsigned int month;
    unsigned int day;
    char *artist;
    char *album;
    };

    question is:
    Read from stdin and populate a fixed sized array
    (not alinked list) of idtags. Write the contents to stdout
    upon request.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    OK. I see your requirements but I don't see your question.

    Exactly where are you stuck?

    Comment

    • kaffekopp
      New Member
      • Feb 2008
      • 2

      #3
      The last part is done.
      this: Write the contents to stdout
      upon request.

      but the first part, where populate a fixed sized array
      (not a linked list) of idtags is where im stuck. Dont't know how to use a sized array. I think that i need to save the structs in the array, thats where im stuck.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Start here:
        [code=cpp]
        idtag arr[10];
        [/code]

        Read this:
        Originally posted by weaknessforcats
        First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
        [code=c]
        int array[5];
        [/code]

        That is an array of 5 elements each of which is an int.

        [code=c]
        int array[];
        [/code]

        won't compile. You need to declare the number of elements.

        Second, this array:
        [code=c]
        int array[5][10];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 int.

        [code=c]
        int array[5][10][15];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


        [code=c]
        int array[][10];
        [/code]

        won't compile. You need to declare the number of elements.

        Third, the name of an array is the address of element 0
        [code=c]
        int array[5];
        [/code]

        Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

        [code=c]
        int array[5][10];
        [/code]

        Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
        [code=c]
        int array[5][10];

        int (*ptr)[10] = array;
        [/code]

        Fourth, when the number of elements is not known at compile time, you create the array dynamically:

        [code=c]
        int* array = new int[value];
        int (*ptr)[10] = new int[value][10];
        int (*ptr)[10][15] = new int[value][10][15];
        [/code]

        In each case value is the number of elements. Any other brackets only describe the elements.

        Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

        [code=c]
        int** ptr = new int[value][10]; //ERROR
        [/code]

        new returns the address of an array of 10 int and that isn't the same as an int**.

        Likewise:
        [code=c]
        int*** ptr = new int[value][10][15]; //ERROR
        [/code]

        new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

        With the above in mind this array:
        [code=cpp]
        int array[10] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Wheras this array:
        [code=cpp]
        int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Kinda the same, right?

        So if your disc file contains

        0 1 2 3 4 5 6 7 8 9

        Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

        Therefore, when you do your read use the address of array[0][0] and read as though you have a
        one-dimensional array and the values will be in the correct locations.
        Then when you have an array of structs, the members are accessed using the member-of operator:
        [code=cpp]
        idtag arr[10];
        arr[0].song....etc;
        [/code]

        In the case of pointer members, be sure to allocate memory for the pointer to point at and then copy the data to that location.

        Post again if you are still hammered.

        Comment

        Working...