enumeration types in C


 
Thread Tools Search this Thread
Top Forums Programming enumeration types in C
# 1  
Old 08-12-2008
enumeration types in C

If I want to declare an array of structures in C and have the number of items in that array to correspond to the items of an enumeration, is there a way to access the maximum value in the enumeration when declaring the array?

For instance:

typedef struct
{
various fields....
} fruit_rec;

enum fruit_info {apple, orange, banana}

fruit_rec fruits[3];


Instead of hard-coding a '3' in the 'fruits' declaration, is there a way I can access the number of items in the enumeration? In the old DoD language, Ada, one could do something like, " fruit_rec fruits[fruit_info] " .

I don't think there is a way to do what I'm asking, but I thought I'd ask as it would be a cleaner way of declaring things. Even if I can't do something like that, I know I can still refer to the elements as "fruits[apple}" , "fruits[orange]", etc. Thank you.
# 2  
Old 08-12-2008
Code:
enum fruit_info {apple, orange, banana, fruit_info_cnt}

fruit_rec fruits[fruit_info_cnt];

How is this?
# 3  
Old 08-12-2008
You could do:
enum {
apple=0,
orange,
bannana,
count}

then count=3
# 4  
Old 08-12-2008
Yes, that will work just fine; I guess I should have thought of something like that before.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cp -r except certain file types

the following excludes certain directories successfully cp -r probe/!(dir) /destination I want to exclude certain file types and tried unsuccessfully cp -r probe/!(*.avi) /destination (2 Replies)
Discussion started by: tmf
2 Replies

2. What is on Your Mind?

What Types of Food Do You Like The Most?

On another simple topic, multiple choice answers OK ( you can pick more than one or suggest others - we will add your suggestions to the poll ). What Types of Food Do You Like The Most? (27 Replies)
Discussion started by: Neo
27 Replies

3. UNIX for Dummies Questions & Answers

Un-compression types...

Hi Folks, As I am familiar wih both types compresion forms: gun-zip and .rpm. My questions is how do I uncompress gunz.zip type? As the .rpm I can double click and it will extract...Can someone shed some light on this and thank you... M (2 Replies)
Discussion started by: Mombo_Z
2 Replies

4. UNIX for Dummies Questions & Answers

mime types

Hi, I am trying to launch an ogg movie from a pdf file which has been produced with pdflatex and \movie {\centerline{\includegraphics {grafiques_xerrades/un_manolo_amb_camera.pdf}}} {hlims_xerrades/XocCumuls.ogg} The switch "externalviewer" makes kpdf launch the default... (5 Replies)
Discussion started by: pau
5 Replies

5. UNIX for Dummies Questions & Answers

Two types of pipes?

What is the difference between: cd /tmp tar -cf - *.txt |gzip > tmp_txt.tar.gz and cd /tmp mknod pipe p gzip < pipe > /tmp/tmp_txt1.tar.gz & tar -cf pipe *.txt Apart from the fact that we have to create the pipe file manually, is there any difference in the performance of the two?... (5 Replies)
Discussion started by: blowtorch
5 Replies

6. Programming

X-Windows Enumeration

Hi, I'm new to x-windows system and I have a few questions: 1. How can I write a C program to enumerate all the windows on the local and remote computers? In other words: How can I create a list of all the windows in the system (maximized/minimized/active/not active ....)? 2. How can I... (0 Replies)
Discussion started by: itaihoe
0 Replies

7. UNIX for Dummies Questions & Answers

So many types of LINUX's!

I installed Redhat into my system. The reason? This was the version my friend was running and he told me about this one, so I downloaded and installed it. Simple enough :D But as I am searching the net, I am coming across many other forms of linux made by other companies. Redhat seems to be... (2 Replies)
Discussion started by: Minnesota Red
2 Replies

8. UNIX for Dummies Questions & Answers

servers types

dear sir , i would like to ask about sun solaries servers generations ? i hear about sparc and ultra . i want to know the versions , and is there other servers types ?? Thank (3 Replies)
Discussion started by: tamemi
3 Replies
Login or Register to Ask a Question