指向 NULL 的结构初始化指针 [英] Pointer of structure initialization to NULL

查看:64
本文介绍了指向 NULL 的结构初始化指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对结构中的结构有问题:

I'm having a problem with a structure within a structure:

typedef struct BrickStruct
{
    int type;
    SDL_Rect Brick_Coordinates;  
    SDL_Surface *Brick_Surface = NULL;  
}BrickStruct; 

我的编译器说关于 SDL_Surface 结构的那一行:

my compiler says that about the line with the SDL_Surface structure:

error: '=' token

但我真的不明白,因为我在老师的课上讲到了结构指针,我说:坐标*point = NULL;

But I don't really understand because I got in front of me my teacher's lesson about pointer of structure saying that: Coordinate *point = NULL;

坐标是一个内部有两个int的结构体:int x,y;

Coordinate being a structure with two int inside: int x,y;

有人能解释一下那个奇怪的东西吗?

Can somebody explain me that weird thing ?

谢谢

推荐答案

C 语言不允许像这样内联实例字段的初始化.标准做法是编写一个工厂风格的方法来为您进行初始化

The C language does not allow for the initialization of instance fields inline like this. The standard practice is to write a factory style method which does the initialization for you

BrickStruct create_brick_struct()
{
  BrickStruct s;
  s.Brick_Surface = NULL;
  s.type = <default type value>;
  s.Brick_Coordinates = <default coordinatos value>;
  return s;
}

这篇关于指向 NULL 的结构初始化指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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