【问题标题】:C - unable to edit or print char arrayC - 无法编辑或打印字符数组
【发布时间】:2014-01-09 23:05:31
【问题描述】:

这里的新手问题...当我在函数中时,我应该如何正确处理表示字符串的可变字符数组?我正在这样做

char temp[BASE10LENGTH_DEGREE_DECIMALS+1]; //aka length of 7
memset(temp, 0, sizeof(temp));

如您所见,它有那个空终止符。但是如果我做这样的事情

temp[i] = '1'; //when i = 0

然后在 temp 上调用 atoi(),我得到 0。编辑 2:不,我没有!但我还是无法在调试器中打印出来。

另外,如果我查看调试器,temp 不会扩展为数组,并且在其上使用 lldb 的打印功能给了我这个

(lldb) print temp
error: incomplete type 'char []' where a complete type is required
error: 1 errors parsing expression

如果我使用 char* 并对其进行 malloc,它会起作用,但这不是我想要做的。我只想要一个 char 数组。我做错了什么?

编辑:这是整个方法。输入为 "3, 4, 5",len 为 7: 编辑 2:实际上,atoi 的问题是因为我通过输入小于 9 且大于 0 而不是小于 9 且大于 0 来搞乱这些 if 语句......粗心的错误。

struct Coordinates{
unsigned int longitude;
unsigned int latitude;
unsigned short altitude;
};

struct Coordinates* getCoordinatesFromString(char* input, int len){ 
struct Coordinates* ret = malloc(sizeof(struct Coordinates));

char temp[BASE10LENGTH_DEGREE_DECIMALS+1];
memset(temp, '\0', sizeof(temp));
int i = 0; int i2 = 0; char currentChar;
while (input[i]!=','){
    if (i>=len)
        return NULL; //out of bounds error
    currentChar = input[i];
    if ((currentChar>=0 && currentChar<=9) || currentChar=='.') temp[i2] = currentChar;
    i++;
    i2++;
}
ret->latitude = atoi(temp);
memset(temp, 0, sizeof(temp));
i++; i2 = 0;
while (input[i]!=','){
    if (i>=len)
        return NULL; //out of bounds error
    currentChar = input[i];
    if ((currentChar>=0 && currentChar<=9) || currentChar=='.') temp[i2] = currentChar;
    i++;
    i2++;
}
ret->longitude = atoi(temp); //keeps giving me zero
memset(temp, 0, sizeof(temp));
i++; i2 = 0;
while (input[i]!=','){
    if (i>=len)
        break;
    currentChar = input[i];
    if ((currentChar>=0 && currentChar<=9) || currentChar=='.') temp[i2] = currentChar;
    i++;
    i2++;
}
ret->altitude = atoi(temp);
memset(temp, 0, sizeof(temp));

return ret;
}

【问题讨论】:

  • 您需要向我们展示出现问题的small complete program。我只是将您的代码复制并粘贴到一个小程序中,atoi(temp) 正确返回了1。见ideone.com/HrByzB
  • temp是局部变量还是函数参数?
  • temp 是一个局部变量,只是在该函数内部声明。
  • 在你的比较中:if ((currentChar&gt;=0 &amp;&amp; currentChar&lt;=9),你的意思是if ((currentChar&gt;='0' &amp;&amp; currentChar&lt;='9')吗?我猜tmp 是空的,因为条件永远不会为真。 If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned. 这可以解释为什么你的输出为零。

标签: c arrays string


【解决方案1】:

当您找到逗号时,您将跳过逗号 (++i),但输入中的下一个输入字符是空格,因此 temp[0] 以空字符结尾,这意味着 atoi()将返回 0。您需要跳过逗号和空格。

或者,如果您的输入字符串以空值结尾,您可以使用 C 运行时库中的 strtok() 函数来简化代码。示例:

#include <string.h>

struct Coordinates* getCoordinatesFromString(char* input)
{ 
    struct Coordinates* ret = malloc(sizeof(struct Coordinates));
    int part = 0;

    if (ret != NULL) {
        char *s = strtok(input, ",");
        while (s != NULL && part < 3) {
            int value = atoi(s);

            switch(++part) {
                case 1:
                    ret->latitude = value;
                    break;
                case 2:
                    ret->longitude = value;
                    break;
                case 3:
                    ret->altitude = value;
                    break;
            }

            s = strtok(NULL, ",");
        }

        /* if input was not valid, return NULL */
        if (part < 3) {
            free(ret);
            ret = NULL;
        }
    }

    return ret;
}

【讨论】:

  • 你是对的。我没有时间多看它,但这解释了为什么它只获得第一个坐标。我仍然无法使用调试器打印它,但我对此并不担心......只是有点好奇。
猜你喜欢
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 2021-12-12
相关资源
最近更新 更多