Converting a char_string to the LONG they are #defined as

Started by
3 comments, last by Zahlman 17 years, 9 months ago
Basically my code currently looks like this:
/**PROC+**********************************************************************/ /* Name: nbb_filter_init */ /* */ /* Purpose: Create array of values to filter from string of values separated by commas. */ /* */ /* Returns: Nothing. */ /* */ /* Params: IN input_array - character array holding comma separated values (corrupted by this function). */ /* OUT output_array - array of values to filter. */ /* OUT num_used - pointer to number of filters. */ /* IN max_num_filters - the maximum number of allowed filters. */ /* */ /* Operation: Scan the list for a comma. Convert the characters up to this point to a filter ID. Record the number of filters found. */ /* */ /**PROC-**********************************************************************/ NBB_VOID nbb_filter_init(NBB_CHAR *input_array, NBB_LONG *output_array, NBB_INT *num_used, NBB_INT max_num_filters) { /***************************************************************************/ /* Local Variables */ /***************************************************************************/ NBB_INT ii; NBB_CHAR *cur_filter; NBB_INT num_filter = 0; /***************************************************************************/ /* Fill in filter array with comma separated entries from the string. */ /***************************************************************************/ for ((cur_filter = strtok(input_array, ",")); ((cur_filter != NULL) && (num_filter < max_num_filters)); (cur_filter = strtok(NULL, ","), num_filter++)) { if ( cur_filter[1] == 'x' || cur_filter[1] == 'X') { /***********************************************************************/ /* Assume number has been prefixed by 0x or 0X */ /***********************************************************************/ cur_filter += 2; } output_array[num_filter] = strtol(cur_filter, NULL, 16); } /***************************************************************************/ /* Fill in rest of filter array with null entries */ /***************************************************************************/ for (ii = num_filter; ii < max_num_filters; ii++) { output_array[ii] = 0; } /***************************************************************************/ /* Fill in number of filters used */ /***************************************************************************/ *num_used = num_filter; return; } /* nbb_filter_init */ [\quote] input array is an array of chars - inputted in a configuration file output array is an array of long numbers - stored for use later num_used is the number of filters found - recorded for use later max_num_filters - used to ensure no arrays are over-filled What I want to be able to do: Instead of inputting a set of long numbers in the configuration file, I want to be able to input a char_string. This char_string is #defined in a header file elsewhere (that will be included) to be a specific long number. Hence instead of looking up the long number associated with an option, I can just type the option name. The above function will then look up what long number I am referring to. Can anybody help me, please :-) [Edited by - explicitlyrics100 on July 21, 2006 11:17:59 AM]
Advertisement
You cannot "look it up from the define", because defines don't work that way. They are just text substitution into your source code; there is no runtime "symbol" generated.

Build a table in the code associating some string literals with numbers, and look up the string at runtime by string comparison, returning the associated number from the table.

If you're smart, you should be able to figure out how to use hashing or binary search to improve performance. However, I have a strict policy now of not giving *detailed* help to people who still insist on writing in C.

The fact that you seem to have typedeffed 'void' makes me even less inclined to help :\
On a quick glance this really looks like a homework assisignment. Remeber we will not do your homework for you.

Just to get you going in a right direction, there are a couple ways this could be done, #define is not one of them. Look up enums and the other way is two arrays one with the strings you are looking up.. and another with the associated long value.

Hope that helps.

theTroll
Im actually a new starter at a fairly big uk company. This is one of my first few tasks and part of me joining is working stuff out myself. I was just looking for any hints really. The codebase I am working on is 2million lines long and I am doing a small modification. In terms of typedef, the program is designed to run on w32/unix/vxworks/ose and all other conceivable OS's, hence all normal C commands have to be assigned macros. These are themselves defined to be specific commands dependant on the OS it is being compiled for. I have already simplified out most of the #defines for clarity but there are a few still there...

That is what I feared about the #defines as they are not accessible by code once the program is running. I cannot make a hash table of the #defines as that is forbidden by the coding standards of my company. I will have another look and see if I can come up with any ideas on monday, when my mind is hopefully fresh...

Thanks
Quote:Original post by explicitlyrics100
Im actually a new starter at a fairly big uk company.... part of me joining is working stuff out myself... The codebase I am working on is 2million lines long and I am doing a small modification... hence all normal C commands have to be assigned macros... I cannot make a hash table of the #defines as that is forbidden by the coding standards of my company.


Might I suggest you quit this job and resume looking for a place that deserves a competent programmer?

This topic is closed to new replies.

Advertisement