468,513 Members | 1,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

NULL struct at end of declared array of structs

I have an array of structs which I will be going through one-by-one to search for a matching value. This array might have elements added and removed later, so I don't want to have to update a defined size for the array. What I'd like to do is put a NULL struct at the end so that the end can be detected.

Here's some code:

Expand|Select|Wrap|Line Numbers
  1. typedef struct smfMolTrans {
  2.   char molecule[SZFITSCARD]; /* molecule species */
  3.   char transiti[SZFITSCARD]; /* transition */
  4.   double freq;               /* frequency of transition */
  5. } molTrans;
  6.  
  7. static molTrans *transitions[] = {
  8. {"SO", "4 5 - 3 4", 178605.403000},
  9. {"CH", "2 -1 3 3 - 2 1 2 2", 178875.247400},
  10. {"SiCC", "8 0 8 - 7 0 7", 179446.496000},
  11. /*.... a bunch more....*/
  12. };
  13.  
Then later I want to be able to do something like
Expand|Select|Wrap|Line Numbers
  1. while ( transitions[i] != NULL ) {
  2. /* check to see if the frequency matches, do some stuff... */
  3. }
  4.  
Basically I need to be able to know when to quit searching through the array. When I try putting a NULL at the end of the array:

Expand|Select|Wrap|Line Numbers
  1. static molTrans transitions[] = {
  2. {"SO", "4 5 - 3 4", 178605.403000},
  3. {"CH", "2 -1 3 3 - 2 1 2 2", 178875.247400},
  4. {"SiCC", "8 0 8 - 7 0 7", 179446.496000},
  5. /*.... a bunch more....*/
  6. NULL
  7. };
  8.  
I get "warning: initialization makes integer from pointer without a cast" (for the initialization of the array) and "error: invalid operands to binary !=" (for the while loop).

Suggestions?
Mar 31 '08 #1
2 3770
Laharl
849 Expert 512MB
One solution is to create a set of values (maybe "", "", and 0) and treat that as your NULL struct. Another is to use a linked list of structs rather than an array. You'd (probably) need another struct that has a moltrans as its data member, along with a pointer to the next node in the list.
Mar 31 '08 #2
weaknessforcats
9,207 Expert Mod 8TB
This array might have elements added and removed later, so I don't want to have to update a defined size for the array.
By definition an array has a fixed size.

That means you have a variable for the number of elements. Use that variable to know when you are at the end of the array.

Only a char array with a NULL gets special treatment as a "string".

Are you using C++? You don't say, but if you are, the vector implements an elastic array that expands and contracts.

Also, instead of an array, could you use a double-linked list?
Mar 31 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by sathyashrayan | last post: by
5 posts views Thread by anonymous | last post: by
6 posts views Thread by S.Tobias | last post: by
5 posts views Thread by Chua Wen Ching | last post: by
4 posts views Thread by DaHool | last post: by
6 posts views Thread by arnuld | last post: by
12 posts views Thread by Milux | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.