本文介绍了初始化结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨! 我有一个很大的结构,我想立刻初始化它,但我得到解析 错误之前{"编译器错误。不能这样做吗? #include< stdio.h> #include< stdlib.h> typedef struct { int b; int c; int d; int e; } A; A * a; int main(void){ a = malloc(4 * sizeof(* a)); a = {1,1,4,5}; //来自我的编译器的错误 printf("%d;%d \ nn,a-> b + a-> d,a-> c * a- > e); 免费(一); 返回0; } 这是初个结构应该怎么做的坏方法? 解决方案 你不能为指针分配静态初始化器。你可以写 A a [4] = {{1},{1},{4},{5}}; - Ian Collins。 不,你有一个指向结构的指针。 这不是特别大...... A b = {1,2,3,4}; 删除这2行,替换为 A * a = b; printf("%d;%d \ n",a-> b + a-> d,a-> c * a-> e); 免费(一); 返回0; } 是这是init struct怎么做的坏方法? 只能作为定义的一部分,就像我上面所做的那样。而不是 指向struct的指针。 再见,Jojo 你不能为指针分配静态初始化器。你可以写 A a [4] = {{1},{1},{4},{5}}; 将[0] .b设置为1,将[1] .b设置为1,将[2] .b设置为4和a [3] .b到5, 留下所有c,d和e成员未初始化的resp。设置为0. A a = {1,1,4,5}; 会这样做...... 再见,乔乔 Hi!I have a big struct and I want to initialize it at once, but I get parseerror before "{" compiler error. can''t it by done?#include <stdio.h>#include <stdlib.h>typedef struct{int b;int c;int d;int e;}A;A *a;int main(void) {a = malloc(4 * sizeof (*a));a = {1,1,4,5}; //there is error from my compilerprintf("%d ; %d\n", a->b+a->d, a->c*a->e);free(a);return 0;}is this is bad way to init struct how one should do? 解决方案You can''t assign a static initialiser to a pointer. You could writeA a[4] = {{1},{1},{4},{5}};--Ian Collins.No, you have a pointer to a struct.That''s not particulary large...A b = {1,2,3,4};drop these 2 lines, replace byA *a = b;Can be done only as part of the definition, like I did above. And not to apointer to struct.Bye, JojoYou can''t assign a static initialiser to a pointer. You could writeA a[4] = {{1},{1},{4},{5}};Which would set a[0].b to 1, a[1].b to 1, a[2].b to 4 and a[3].b to 5,leaving all the c, d and e members uninitialized resp. set to 0.A a = {1, 1, 4, 5};Would do...Bye, Jojo 这篇关于初始化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 03:11