c - 指针错误

标签 c arrays pointers struct

typedef struct {
    char unit1[3];  // unit1 has value ns
    char unit2[3];  // unit2 has value ns
    char unit3[3];  // unit3 has value ns
} unit;

unit u;

我有一个全局结构,它有 3 个大小为 3 的字符数组,并且其中包含值“ns\0”。 Null 终止,因此大小为 3。

/* This function checks if all has same unit */
int check_conversion_unit() {
    if ( (u.unit1[0] == u.unit2[0]) && (u.unit2[0] == u.unit3[0]))     // ERROR HERE
        return 1;
    else
        return 0;

}

一些其他函数调用此函数来在计算之前检查所有单元是否具有相同的大小。但是,当我尝试编译此代码时,出现如下错误:

error: subscripted value is neither array nor pointer nor vector
error: subscripted value is neither array nor pointer nor vector
error: subscripted value is neither array nor pointer nor vector

最佳答案

你在这里忘记了):

if ( (u.unit1[0] == u.unit2[0]) && (u.unit2[0] == u.unit3[0]) )  
//                                                            ^
//                                                            | that's one

关于c - 指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321854/

相关文章:

c - 为什么不能重新分配字符串数组,但是字符串指针可以使用C语言?

c - 时域重叠相加如何实现时间拉伸(stretch)?

c - 使用偏移量取消引用多级指针的最简洁方法是什么?

javascript - JavaScript 中为什么有数组?对象还不够吗?

c++ - 如何像我以前在 C++ 中做的那样在 swift 中做指针

c++ - 将成员函数指针与 0 进行比较是否总是有效的 C++?

c - 程序编译但没有给出正确的值

c - 无法将堆栈大小初始化为 0

javascript - 清理代码的最佳方法是什么?

arrays - 查找数组中出现奇数次的所有元素