2

What I want to do is simply initialize a instance of a struct. The struct is defined here:

typedef struct Rectangle 
{
tName Name; /* name of the rectangle */
struct Rectangle * binSon[2];
int Center[2];
int Length[2]; /* distance to the boarder of the rectangle */
int Label;
}Rectangle;

and how I initialized it is like below:

Rectangle * binSon[2];
binSon[0] = NULL;
binSon[1] = NULL;

int Center[2];  
int Length[2];

Center[0] = 0;
Center[1] = 0;
Length[0] = 5;
Length[1] = 5;

Rectangle world = {"World", binSon, Center, Length, 0};

at the last line, when I compile the program, it reports me an warning of:

mx_cif_ds_manager.c:52:2: warning: initialization makes integer from pointer without a cast [enabled by default]
mx_cif_ds_manager.c:52:2: warning: (near initialization for 'world.Name[0]') [enabled by default]
mx_cif_ds_manager.c:52:2: warning: initialization makes integer from pointer without a cast [enabled by default]
mx_cif_ds_manager.c:52:2: warning: (near initialization for 'world.Name[1]') [enabled by default]
mx_cif_ds_manager.c:52:2: warning: initialization makes integer from pointer without a cast [enabled by default]
mx_cif_ds_manager.c:52:2: warning: (near initialization for 'world.Name[2]') [enabled by default]
mx_cif_ds_manager.c:52:2: warning: initialization makes integer from pointer without a cast [enabled by default]
mx_cif_ds_manager.c:52:2: warning: (near initialization for 'world.Name[3]') [enabled by default]

Not sure what's going wrong with this piece of program, and wondering if any one has any idea or suggestion that can help me improve this piece of code. Whats a more gental way to initialize a struct?

Thank you

======UPDATE======

Thank you for your help, but what if I want to use variables to initialize a struct. Do I have to malloc a memory space in this case?

The tName definition is here

typedef char tName[MAX_NAME_LEN + 1];
Allan Jiang
  • 11,063
  • 27
  • 104
  • 165

3 Answers3

15

This should work:

Rectangle world = {"World", {NULL, NULL}, {0, 0}, {5, 5}, 0};
Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • Hi, thank you for your answer, but I did not see the difference between your answer and mine. it seems like you just put everything in one line. – Allan Jiang Sep 25 '12 at 17:00
  • 3
    @AllanJiang You can't use variables in an initializer list, it has to be literals. – Some programmer dude Sep 25 '12 at 17:02
  • 1
    @JoachimPileborg Thank you, but what if I want to use variables later, how I can initialize the struct with some variables in this case? – Allan Jiang Sep 25 '12 at 17:07
  • @AllanJiang You're confusing the concepts of **initial** ization and assignment. You can always assign values to your struct fields later. – Jim Balter Sep 25 '12 at 19:16
4

Much simpler:

Rectangle world = { "World", { NULL, NULL }, { 0, 0 }, { 5, 5 }, 0 };
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

To expand on Keith's answer, you could also do this to use your variables if you wanted. Note this is only valid if you are within a function.

void foo(void)
{
    Rectangle world = {"World", {binSon[0], binSon[1]}, {Center[0], Center[1]}, {Length[0], Length[1]}, 0};
}
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • No, you can't do that ... the initializers must be constant if the struct is at file scope. And even if you could, it just feeds into the OP's confusion between initialization and assignment. – Jim Balter Sep 25 '12 at 19:18
  • @JimBalter, the OP wasn't explicit about the scope. From the question this could have just as easily been performed in a function (which is what I saw). – Josh Petitt Sep 25 '12 at 19:54
  • @JimBalter and the OP also I don't know if what I posted requires C99. That's what I typically use. It may not work for ANSI C. – Josh Petitt Sep 25 '12 at 19:57