473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 4419
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,208 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

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

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
5
by: anonymous | last post by:
I have a Lex file containing definitions of 2 structures like: %{ struct a {...}; struct b { struct a i; ... }; struct a x;
6
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is ...
5
by: Chua Wen Ching | last post by:
Hi there, I am very curious on this code: // declared as structure public struct Overlapped { public IntPtr intrnal; public IntPtr internalHigh;
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
12
by: Milux | last post by:
Hi All, This question has to do with interface design. Suppose I have an translation tool. It can translates structs from type "general" to other types and then does some processing. Example:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.