Structure definitions

Discussion in 'C++' started by crystal twix, Nov 8, 2009.

  1. crystal twix

    crystal twix Guest

    As part of a homework problem, I'm trying to declare a new struct
    within the definition, but my new definition is an array, and I am
    only to define some of the values. For example:

    #define STUDENTS 100

    struct Test {
    int score;
    int grade;
    } score[STUDENTS], ???

    but I want to only initialize the first 5. Thanks!
    crystal twix, Nov 8, 2009
    #1
    1. Advertising

  2. On Nov 8, 3:06 am, crystal twix <> wrote:
    > As part of a homework problem, I'm trying to declare a new struct
    > within the definition, but my new definition is an array, and I am
    > only to define some of the values.  For example:
    >
    > #define STUDENTS 100
    >
    > struct Test {
    >   int score;
    >   int grade;
    >
    > } score[STUDENTS], ???
    >
    > but I want to only initialize the first 5.  Thanks!


    Hi
    Because it is a homework, I should give you just few hints:
    1. Use const types rather than macros:
    const int STUDENTS = 100;
    2. The name of your array and one of its struct members are same:
    score. use different names.
    3. You can intialize the first 5 array elements using array
    initilizers list
    almost in the same way as initializing array of integers:
    int a[100] = { 0, 1, 2, 3, 4 };

    Good luck
    -- Saeed Amrollahi
    Saeed Amrollahi, Nov 8, 2009
    #2
    1. Advertising

  3. crystal twix

    Jorgen Grahn Guest

    On Sun, 2009-11-08, crystal twix wrote:
    > As part of a homework problem, I'm trying to declare a new struct
    > within the definition, but my new definition is an array, and I am
    > only to define some of the values. For example:
    >
    > #define STUDENTS 100
    >
    > struct Test {
    > int score;
    > int grade;
    > } score[STUDENTS], ???
    >
    > but I want to only initialize the first 5. Thanks!


    I assume your homework is a C++ assignment since you are posting here.
    Use std::vector<Test> instead of a C-style array, and make Test a
    class with a constructor.

    (And if your teacher is not aware of the standard containers,
    quit the class and buy a good C++ book instead.)

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 8, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mister
    Replies:
    5
    Views:
    502
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    740
    Martin Ambuhl
    Oct 26, 2004
  3. Leo Nunez
    Replies:
    3
    Views:
    1,193
    Neil Kurzman
    Feb 9, 2005
  4. Replies:
    2
    Views:
    592
  5. hotadvice
    Replies:
    14
    Views:
    698
    hotadvice
    Oct 2, 2007
Loading...

Share This Page