Sunday, January 25, 2026

Using a Simple C While-Loop to Step Throught Option Arrays

Sometimes its necessary to run through a GnoclOption array. As such arrays are always NULL terminated, a simple while loop solves the problem.


gint idx = 0; 

while ( tagOptions[idx++].optName != NULL )  {

g_print ( "*%d %s\n", idx, tagOptions[idx].optName );

if ( strcmp ( tagOptions[idx].optName, "-myOption" ) ) == 0 ) {

GNOCL_DEBUG_MSG ( "got it!" )

break;

 }

 

As can be seem in the above example, a GnoclOption array is an array of structures. 

 

If just the array size is needed, then simply use the macro GNOCL_TOTAL_OPTIONS(option) to return the length, less the NULL, of course.

The macro ARRAY_SIZE(x) will provide the overall size of an array, including any NULL termination. 

 

 

No comments: