在C

时间:2016-10-30 21:36:18

标签: c arrays parsing struct command-line-arguments

我正在编写一个简单的struct array程序。给出一个字符串,我想解析它。一个字符串由几个字符组成。

例如,字符串“a:bc:D:E”有5个唯一字符。冒号“:”表示该角色有值。

结构数组大小为256((选项[256])),其中包括所有ASCII字符。

从给定的字符串中,我想找到字符并在ASCII位置填充值为“1”的struct数组。如果字符串中不存在字符,则赋值“0”。

此外我想设置此结构的“hasVal”字段。例如,a = 1(在给定字符串中有冒号),b = 0(字符串中“b”后没有冒号),c = 1, D = 1,E = 1.

最后,打印此结构,如预期输出中所示。

我的编程不是很好。我刚开始学习C语言。我试过这个,但我没有得到预期的结果。如果我无法传达我的问题陈述,我会道歉。

非常感谢任何帮助。提前谢谢。

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;
}

实际输出:

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

此行之后没有得到任何东西。

预期产出:

 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

2 个答案:

答案 0 :(得分:1)

首先,在您的主要结尾处,您尝试将i打印为char,将option[...].hasVal打印为int,是不是倒置了?< / p>

不是*(optStr + i),而是optStr[i]。它完全相同,而且更具可读性。

然后,option是validOpt的数组。 通过这样做:

option[*(optStr+i)] = 1;

*(optStr+i) == optStr[i]是一个字符,所以它是一个ascii值。 然后尝试将validOpt分配给1.您不能。因为validOpt是一个结构。也许你想做validOpt.hasValue = 1

事实上,我并不完全明白你想在这里做什么,如果你能发展得多一点就会很棒:)

答案 1 :(得分:0)

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

这种情况还不够。您需要区分出现和未出现在字符串中的字符,以及具有或不具有值的字符。

您需要一个额外的变量,例如is_shown来判断角色是否出现。

#define MAX_CHAR 256

char范围从零到128.在您的示例中,似乎从A转到z就足够了。但是让它保持在128。

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

上面有一个错误。当你到达字符串中的最后一个字符时,再次递增以检查下一个字符,它就会越过边界。

修改代码如下:

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;
}

输出如下:

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

你必须按照你想要的方式对其进行排序。请注意,E未设置,因为:后面没有E,这是最后一个字符。

编辑:替代方法:

使用malloc(strlen(optStr) * sizeof(validOpt))创建option数组并填充它。在这种情况下,option的数组大小为8,它只需要数组大小5.它的效率更高。

typedef struct {
    char ch;
    int hasVal;
} validOpt;

int main()
{
    char *optStr = "a:bc:D:E";

    //option's array size is always larger than optStr's size, so it's safe
    validOpt *option = malloc(strlen(optStr) * sizeof(validOpt));

    int i, count = 0, len = strlen(optStr);
    for (i = 0; i < len - 1; i++)
    {
        if (optStr[i] == ':') //ignore this character
            continue;
        option[count].hasVal = optStr[i + 1] == ':';
        option[count].ch = optStr[i];
        count++;
    }

    if (optStr[i] != ':') //handle the last character in optStr
    {
        option[count].hasVal = 0;
        option[count].ch = optStr[i];
        count++;
    }

    for (i = 0; i < count; i++)
        printf(" %c -- %d\n", option[i].ch, option[i].hasVal);
    free(option);
    return 0;
}

输出:

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