Structures or Array Notation for many possible strings of finite length?

G

Guest

I have an application that processes 30 kinds of strings of that fit
in an array.

By processing, I mean
-check the value of certain characters or substrings to be
within contant bounds or a member of a list
-check the value of certain characters or substrings to be
within bounds defined by other characters or substrings
-call a series of functions based on value of certain
substrings.

Each kind of string has a different length, but it fits within an
array of 128 char's


Would you do the processing with array notation (using #defines for
the array indices) in order to write the clearest code , or would you
setup a union of 30 structs (one struct for each kind of sentence) to
write clearest code?
 
M

Malcolm

nospam said:
I have an application that processes 30 kinds of strings of that fit
in an array.

By processing, I mean
-check the value of certain characters or substrings to be
within contant bounds or a member of a list
-check the value of certain characters or substrings to be
within bounds defined by other characters or substrings
-call a series of functions based on value of certain
substrings.

Each kind of string has a different length, but it fits within an
array of 128 char's


Would you do the processing with array notation (using #defines for
the array indices) in order to write the clearest code , or would you
setup a union of 30 structs (one struct for each kind of sentence) to
write clearest code?
A union of 30 types is pretty unwieldy, and generally unions are a bad idea.

The string itself fits in an array, then presumably it needs to be tagged
with a type identifier. So I'd declare

typedef struct
{
char text[128];
int type;
} SENTENCE;

then
#define SIMPLEVERB 1
#define COMPOUNDSUBJUNCTIVE 2

or whatever you need.

Some code can presumable work on all sentences, some only on a subset. So
examine the type meber and process as appropriate.
 
B

Barry Schwarz

I have an application that processes 30 kinds of strings of that fit
in an array.

By processing, I mean
-check the value of certain characters or substrings to be
within contant bounds or a member of a list
-check the value of certain characters or substrings to be
within bounds defined by other characters or substrings
-call a series of functions based on value of certain
substrings.

Each kind of string has a different length, but it fits within an
array of 128 char's


Would you do the processing with array notation (using #defines for
the array indices) in order to write the clearest code , or would you
setup a union of 30 structs (one struct for each kind of sentence) to
write clearest code?

Which do you find easier to code and read later?

union{
struct {char c1[L1]}s1;
...
struct {char c30[L30]}s30;
}u
...
if (u.s19.c19[5] == 'x'){...}

or

#define POS19 5
char c[128];
...
if (c[POS19] == 'x'){...}


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top