c - * 对于结构是非法的吗?

标签 c struct pointers

我试图编译以下代码,但编译器不会这样做,因为“* 对于结构是非法的”是真的吗?

struct String {
    int length;
    int capacity;
    unsigned check;
    char ptr[0];
} String;

void main(){

    char *s;
    String *new_string = malloc(sizeof(String) + 10 + 1);

}

最佳答案

要么使用 typedef:

typedef struct String {
    int length;
    int capacity;
    unsigned check;
    char ptr[0];
} String;    /* now String is a type */

或者明确地说struct String:

void main(){
    char *s;
    struct String *new_string = malloc(sizeof(struct String) + 10 + 1);
}

关于c - * 对于结构是非法的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622323/

相关文章:

c - C 中的逻辑运算符导致我的循环出现问题?

c++ - 为什么变量 myOperator 没有显示内存位置,为什么没有正确传递

c++ - 在另一个指向结构的指针内访问结构指针内的元素

c - 确保作为参数传递的字符串不会导致溢出

C++ Int 位操作是 2UL = 10UL?

c - 将结构指针数组传递给函数

c++ - for循环跳过getline

c++ - 显示结构/类错误数组 C++

c++ - 如何在c++中检索 vector 内的对象

c - 如何将整数转换为 LARGE_INTEGER