Struct with array of structs of unknown size

后端 未结 5 537
臣服心动
臣服心动 2021-02-05 12:35

I\'ve been trying to wrap my head around this the whole day...

Basically, I have a struct called State that has a name and another one called StateMachine with a name, a

5条回答
  •  长情又很酷
    2021-02-05 13:28

    Inside of your add_state function:

    temp = malloc(sizeof(struct StateMachine)); 
    

    should be

    temp = malloc(sizeof(struct State));
    

    However, even when this is changed, I still get the proper output:

    --> [0] state: Init
    --> [1] state: Foo
    --> [2] state: Bar
    

    Perhaps there's nothing wrong with your code. I'm using gcc version 4.4.3

提交回复
热议问题