From 80992ebe6c12cfd9d0d03df6762c2c0f121e6351 Mon Sep 17 00:00:00 2001 From: Ian C Date: Tue, 31 Jul 2018 10:46:39 +0000 Subject: Updated structure to use a union. --- codeword.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/codeword.c b/codeword.c index 4080c7f..0a2be5a 100644 --- a/codeword.c +++ b/codeword.c @@ -34,8 +34,11 @@ typedef enum typedef struct { CharacterClass type; - int number; - char letter; + union + { + int number; + char letter; + } data; } Character; static void PrepLine(char *p) @@ -66,7 +69,7 @@ static int ContainsLetter(char c, const Character *chars, int no_chars) for(f=0; f < no_chars; f++) { - if (chars[f].type == eLetter && chars[f].letter == c) + if (chars[f].type == eLetter && chars[f].data.letter == c) { return 1; } @@ -90,7 +93,7 @@ static int CheckLettersAndLength(const char *buff, for(f=0; f < no_chars; f++) { if (chars[f].type == eLetter && - (chars[f].letter != buff[f])) + (chars[f].data.letter != buff[f])) { return 0; } @@ -109,7 +112,7 @@ static int CheckNumbers(const char *buff, const Character *chars, int no_chars) { if (chars[f].type == eNumber) { - int i = chars[f].number - 1; + int i = chars[f].data.number - 1; if (ContainsLetter(buff[f], chars, no_chars)) { @@ -169,7 +172,7 @@ int CheckAnagram(const char *buff, const Character *chars, int no_chars) { if (!used[i] && chars[i].type == eLetter && - chars[i].letter == buff[f]) + chars[i].data.letter == buff[f]) { used[i] = 1; found = 1; @@ -251,11 +254,10 @@ int main(int argc, char *argv[]) if (i < 1 || i > 27) { chars[f].type = eLetter; - chars[f].letter = + chars[f].data.letter = tolower((unsigned char)argv[f + 1 + argc_base][0]); - chars[f].number = 0; - if (chars[f].letter == '.') + if (chars[f].data.letter == '.') { chars[f].type = eMissing; } @@ -269,8 +271,7 @@ int main(int argc, char *argv[]) } chars[f].type = eNumber; - chars[f].number = i; - chars[f].letter = 0; + chars[f].data.number = i; } } -- cgit v1.2.3