Please can someone tell me the difference between a struct and an enumeration?

Recommended Answers

All 15 Replies

Example of a structure:

struct book {
    char title[25];
    char author[25];
    char publisher[25];
    int price;
};


Example of an enum(eration):

enum book {
    BIG,
    SMALL,
};

Why don't you just post your whole exam, since you seem to know a lot about what questions ARE going to be on it.
http://www.daniweb.com/forums/post870168-3.html

Better yet, just give us the email address of the examiner, and we can forward the answers directly, and cut out the middle-man.

> Please can someone tell me the difference between a struct and an enumeration?
YOU tell US what you think the answer should be, then it makes it much less likely for us to consider you a sponge.

Let's just all calm down...
He has just asked the answers for about less than 10 out of
40 questions...Everone hopes, you follow post # 3

YOU tell US what you think the answer should be

And if your answer is wrong, don't worry, everyone will teach you how to get to the right answer.

As of now, have these:

Structs are structures, obviously, structure holds related
elements together in one struct variable
. Just like:
emerald, ruby, sapphire is related to Gems. So you may say;

struct gem_vars {
   int emerald_count;
   int ruby_count;
   int sapphire_count;
}

Now, for enumeration. we enumerate constants with
values in chronological order.
Instead of saying:

#define JANUARY 1
#define FEBRUARY 2
#define MARCH 3
#define APRIL 4

we can simplify it as

enum {
  JANUARY = 1, FEBRUARY, MARCH, APRIL;
}

If still, you didn't get the point, you better read C books
or ask someone else for you to understand...

Thanks.

commented: Nice approach :) +4

Sorry, I forgot to place a semicolon after the closing brace
on my examples...Sorry too for multiple posts.

Sorry, I forgot to place a semicolon after the closing brace
on my examples...Sorry too for multiple posts.

Then just edit your post :)

Sorry, I forgot to place a semicolon after the closing brace
on my examples...Sorry too for multiple posts.

thank your for your simple answers dude. I just got it.

enum {
  JANUARY = 1, FEBRUARY, MARCH, APRIL;
};

I think you aren't much with an enumeration like this, if your enumeration (the data type) hasn't got a name, then you should declare a 'variable' directly after it's definition (you don't have to do this, syntactically your example is correct, but if you don't do this it's pretty much useless: you could just leave it's declaration out of your code because you cannot use it, because you've no instance of it)

I think you aren't much with an enumeration like this, if your enumeration (the data type) hasn't got a name, then you should declare a 'variable' directly after it's definition (you don't have to do this, syntactically your example is correct, but if you don't do this it's pretty much useless: you could just leave it's declaration out of your code because you cannot use it, because you've no instance of it)

It saves keystrokes and if used correctly, can increase code readability.

enum {
  A = 1,
  B = 2,
  C = 3
};
#define A 1
#define B 2
#define C 3

enum method saves keystrokes and is more understandable IMO.

enum method saves keystrokes and is more understandable IMO.

Oh, right I forgot about that, and another addition: #define is deprecated for constants:

#define A 10
#define B 20
#define C A+B
int result=2*C; // won't give 60 as output

Oh, right I forgot about that, and another addition: #define is deprecated for constants:

#define A 10
#define B 20
#define C A+B
int result=2*C; // won't give 60 as output

That's why you are supposed to wrap them in brackets.

#define A 10
#define B 20
#define C (A + B)
int result = 2 * C; // will give 60

That's why you are supposed to wrap them in brackets.

#define A 10
#define B 20
#define C (A + B)
int result = 2 * C; // will give 60

I know, but if you do the same, with constants declared with const for example, you don't have to bother with braces :)

Saving keystrokes is the worst reason. That way you get statements like x1=-b+(sqrt((b*b)-(4*a*c))/(2*a)); rather than x1 = -b + ( sqrt( (b*b) - (4*a*c) ) / (2*a) ); and are prone to make parenthetical mistakes at the very least and write other indecipherable code.

AAMOF I find it faster to do the #defines because of cut/paste. It takes 13 keystrokes for me to get

#define
#define
#define
#define
#define

Then add the definitions from the bottom up. Much faster.

AAMOF I find it faster to do the #defines because of cut/paste. It takes 13 keystrokes for me to get

#define
#define
#define
#define
#define

Then add the definitions from the bottom up. Much faster.

Is that a reason to avoid using the const keyword? I don't think so ...

BTW, If you take the same approach, but with const instead of #define it will even save you more keystrokes :P

Is that a reason to avoid using the const keyword? I don't think so ...

Never said so. I was not commenting on const vs. #define . It was the enum/#define comment that prompted my response -- and the silly keystroke comment therein..

Never said so. I was not commenting on const vs. #define . It was the enum/#define comment that prompted my response -- and the silly keystroke comment therein..

Sorry, I must have misinterpret your post :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.