初始化结构 [英] initializing struct
问题描述
嗨!
我有一个很大的结构,我想立刻初始化它,但我得到解析
错误之前{"编译器错误。不能这样做吗?
#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;
}
这是初个结构应该怎么做的坏方法?
Carramba写道:
嗨!
我有一个大结构,我想立刻初始化它,但我得到解析
错误之前{"编译器错误。不能这样做吗?
#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}; //我的编译器有错误
你不能为指针分配静态初始化器。你可以写
A a [4] = {{1},{1},{4},{5}};
-
Ian Collins。
" Carramba" < ca ****** @ example.comschrieb im Newsbeitrag
新闻:f4 ********** @ aioe.org ...
嗨!
我有一个很大的结构,我想立刻初始化它,但我得到解析
不,你有一个指向结构的指针。
之前错误{"编译器错误。不能这样做吗?
#include< stdio.h>
#include< stdlib.h>
typedef struct {
int b;
int c;
int d;
int e;
} A;
这不是特别大......
>
A * a;
A b = {1,2,3,4};
int main(void) {
a = malloc(4 * sizeof(* a));
a = {1,1,4,5}; //我的编译器有错误
删除这2行,替换为
A * a = b;
< blockquote class =post_quotes>
printf("%d;%d \ n",a-> b + a-> d,a-> c * a-> e);
免费(一);
返回0;
}
是这是init struct怎么做的坏方法?
只能作为定义的一部分,就像我上面所做的那样。而不是
指向struct的指针。
再见,Jojo
" Ian Collins" ; < ia ****** @ hotmail.comschrieb im Newsbeitrag
新闻:5d ************** @ mid.individual.net ...
Carramba写道:
>嗨!
我有一个很大的结构,我想要一次初始化它,但我在{之前得到解析
错误编译器错误。不能这样做吗?
#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}; //我的编译器有错误
你不能为指针分配静态初始化器。你可以写
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 parse
error 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 compiler
printf("%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?
Carramba wrote:Hi!
I have a big struct and I want to initialize it at once, but I get parse
error 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 compilerYou can''t assign a static initialiser to a pointer. You could write
A a[4] = {{1},{1},{4},{5}};
--
Ian Collins.
"Carramba" <ca******@example.comschrieb im Newsbeitrag
news:f4**********@aioe.org...Hi!
I have a big struct and I want to initialize it at once, but I get parseNo, you have a pointer to a struct.
error 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;That''s not particulary large...
>
A *a;A b = {1,2,3,4};
int main(void) {
a = malloc(4 * sizeof (*a));
a = {1,1,4,5}; //there is error from my compilerdrop these 2 lines, replace by
A *a = b;
printf("%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?Can be done only as part of the definition, like I did above. And not to a
pointer to struct.
Bye, Jojo
"Ian Collins" <ia******@hotmail.comschrieb im Newsbeitrag
news:5d**************@mid.individual.net...Carramba wrote:>Hi!
I have a big struct and I want to initialize it at once, but I get parse
error 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 compiler
You can''t assign a static initialiser to a pointer. You could write
A 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
这篇关于初始化结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!