c - 数组的行为

标签 c arrays

我有一个代码如下:

typedef struct Details {
    char a[32];
    char b[32];
    char c[32];
} Details_t;

char *xyz(Details_t *pdetails) {
    if ((NULL == pdetails->a) && (NULL == pdetails->b)) {
        return NULL;
    }
    int len = 0;
    char *newString = NULL;
    len = strlen(a) + strlen(b);
    newString = (char *)calloc(1, len + 3);
    strcpy(newString, a);
    strcat(newString, ";");
    strcat(newString, b);
    strcat(newString, ";");

    return newString;
}

现在我从 main() 传递这个结构的地址。

main() {
    char *ret = NULL;
    Details_t var;
    memset((void *)&var, '\0', sizeof(Details_t));
    strcpy(var.b, "EXAMPLE");
    ret = xyz(&var);
    printf("OUTPUT==%s\n", ret);
}

我的问题是:我没有复制成员 a 中的任何值并且我有 memset() 结构详细信息 NULL 所以所有未复制的成员都应该是无效的。但是在 xyz 函数中,下面的条件失败了。

if ((NULL == pdetails->a) && (NULL == pdetails->b))

我得到的输出如下:

输出==;示例;

为什么这个条件失败了?

最佳答案

当执行此 Details_t var; 时,var.a 和 var.b 拥有自己的地址。所以 var.a == NULL 将返回 false。

关于c - 数组的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17423371/

相关文章:

c++ - 鼠标过滤器驱动程序的问题

c - 交换两个相邻元素的功能不起作用

c++ - 错误 : classname does not name a type

java - Java 十六进制字符串转换为字节

c - 具有函数组合的大 O 表示法

c - 在不声明 char[] 的情况下在 C 中反转字符串的程序

c - 使用 sscanf 解析不会保留数组以供后续使用

C++ std::sort 函数没有完成?

c - Makefile 编译错误未应用 -std=c99

php - 统一命名方案问题: SQL column names and PHP array keys casing schemes conflict