gpt4 book ai didi

c - 初始化指向结构的指针的元素

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:22 25 4
gpt4 key购买 nike

我有以下结构:

typedef struct{
int freq;
char val;
} Char; // alias

我需要创建一个缓冲区(指针)到一定数量的 Char 如下:

Char* chars  = calloc(256, sizeof(Char));

我想将 chars 中所有 Char 结构的 freq 初始化为 -1。是否可以不使用循环来实现?

最佳答案

如果您使用 GCC,您可以执行 Char s[256] = {[0 ... 255] = {-1, 0}};

{-1, 0}为结构初始化

编辑:你可以看看这篇文章: How to initialize all members of an array to the same value?它会给出很好的提示 :)

关于c - 初始化指向结构的指针的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35807225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com