ronme026 ronme026 - 1 month ago 8
C Question

Set struct array in C

I am writing a simple struct array program. A string is given and I want to parse it. A string consists of few characters.

For example, A string "a:bc:D:E" has 5 unique characters. Colon ":" tells that that character has a value.

Struct array size is 256 ((option[256])) which includes all ASCII characters.

From given string, I want to find the characters and fill the struct array with value “1” at their ASCII position. If character is not present in the string then assign the value “0”.

Further I want to set “hasVal" filed of this struct. For example, a = 1 (has colon in the given string), b = 0 (no colon after "b" in the string), c =1, D = 1, E =1.

Lastly, print this structure as shown in the expected output.

I am not very good in programming. I just started learning C language. I tried this but i am not getting expected result. I apologize if I am not able to convey my problem statement.

Any help is much appreciated. Thanks in advance.

sample.c

#include <stdio.h>
#include <stdlib.h>

#define MAX_CHAR 256

typedef struct {
int hasVal;
char *defaultVal;
char *desc;
} validOpt;

validOpt option[MAX_CHAR] = {};

char *optStr = "a:bc:D:E";


int main() {

int i;
for(i = 0; *(optStr + i); i++)
{
/* Not Sure how to check this....
* check the "char" and ":",
* if both are present, set the field "hasVal" to 1 or "0".
*/
if((optStr[i]++) == ":")
option[optStr[i]--].hasVal = 1;
else
option[optStr[i]--].hasVal = 0;

}
printf(“Printing structure…\n”);
printf("\n");
for(i=0; i< MAX_CHAR; i++)
{
if(option[optStr[i]].hasVal == 1) {
printf(" %d -- %c\n", i , option[optStr[i]].hasVal);
}

}

return 0;
}


Actual Output:

[rock12/C_Prog]$ ./sample
Printing structure…


Not getting anything after this line.

Expected Output:

1) If user enters invalid character, give an error.
For Example, "q" -> not valid option

2) For Valid options, print:
a - 1
b - 0
c - 1
D - 1
E - 1

Answer
if(option[optStr[i]].hasVal == 1) {
    printf(" %d -- %c\n", i , option[optStr[i]].hasVal);

This condition is not enough. You need to distinguish between characters which appear and don't appear in the string, and the characters which have or don't have values.

You need an additional variable, for example is_shown to figure out if the character occurs or not.

#define MAX_CHAR 256

char range is from zero to 128. In your example it seems it is sufficient to go from A to z. But let's keep it at 128.

for(i = 0; *(optStr + i); i++)
{
    if((optStr[i]++) == ":")
    {
        ...
    }
}

There is an error above. When you reach the last character in the string you increment once more to check the next characters, it goes over bound.

Modify the code as follows:

typedef struct
{
    int is_show;
    int hasVal;
    char *defaultVal;
    char *desc;
} validOpt;

#define MAX_CHAR 128
int main()
{
    validOpt option[MAX_CHAR] = { 0 };
    char *optStr = "a:bc:D:E";
    while (*optStr)
    {
        char ch = *optStr;
        option[ch].hasVal = 0;
        if (ch != ':')
            option[ch].is_show = 1;

        if (*(optStr + 1))
            if (*(optStr + 1)== ':')
                option[ch].hasVal = 1;

        *optStr++;
    }

    printf("Printing structure\n\n");
    int i;
    for (i = 0; i < 128; i++)
        if (option[i].is_show == 1)
            printf(" %c -- %d\n", i, option[i].hasVal);
    return 0;
}

Output is as follows:

 D -- 1
 E -- 0
 a -- 1
 b -- 0
 c -- 1

You have to sort it to get it the way you want. Note that E is not set, because there is no : after E which is the last character.