初始化 C 结构需要一个表达式 [英] Initialising C struct expected an expression

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

问题描述

我有一个包含大约 17 个成员的 C 结构 struct settings currentProfile 并且想将它的所有成员初始化为零.(我已经尝试过使用 uber-correct struct 语法和 typedef 语法)

I have a C struct with about 17 members struct settings currentProfile and would like to initialise it all members to zero. (I have tried this both with the uber-correct struct syntax and with typedef syntax)

要将所有成员设置为零,我使用 currentProfile = {0}

To set all members to zero I use currentProfile = {0}

在这一行,编译器给出了错误 Expected an expression

at this line the compiler gives the erro Expected an expression

我是否正确初始化?谢谢

Am I initialising correctly? Thanks

推荐答案

您正在执行(无效)分配而不是初始化.

You are doing an (invalid) assignment not an initialization.

用所有成员设置为 0 来初始化你的结构对象:

To initialize your struct object with all members set to 0:

struct settings currentProfile = {0}; 

在声明后将结构体对象的所有成员设置为0:

To set all the members of the struct object to 0 after its declaration:

memset(&currentProfile, 0, sizeof currentProfile);

这篇关于初始化 C 结构需要一个表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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