Structure definitions

C

crystal twix

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!
 
S

Saeed Amrollahi

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
 
J

Jorgen Grahn

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top