*是一个结构是否违法? [英] * is illegal for a struct?

查看:192
本文介绍了*是一个结构是否违法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译以下code,但是编译器不会做,因为*是一个结构违法是真的吗?

 结构字符串{
    INT长;
    INT能力;
    无符号的检查;
    炭ptr的[0];
}字符串;无效的主要(){    字符* S;
    字符串* new_string =的malloc(sizeof的(字符串)+ 10 + 1);}


解决方案

要么使用一个typedef:

  typedef结构{字符串
    INT长;
    INT能力;
    无符号的检查;
    炭ptr的[0];
}字符串; / *现在的String类型* /

或者显式地说结构字符串

 无效的主要(){
    字符* S;
    结构字符串* new_string =的malloc(sizeof的(结构字符串)+ 10 + 1);
}

I tried to compile the following code, but the compiler wouldn't doing because " * is illegal for a struct" is that true?

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

void main(){

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

}

解决方案

Either use a typedef:

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

Or explictly say struct String:

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

这篇关于*是一个结构是否违法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆