Can the address of a variable with automatic storage duration be taken in its definition?

前端 未结 3 1910
不知归路
不知归路 2021-01-14 17:08

Is it allowed to take the address of an object on the right hand-side of its definition, as happens in foo() below:

typedef struct { char x[100]         


        
3条回答
  •  攒了一身酷
    2021-01-14 17:23

    The question has already been answered, but for reference, it doesn't make much sense. This is how you would write the code:

    typedef struct { char x[100]; } chars;
    
    chars make (void) {
      chars c;
      /* init c */
      return c;
    }
    
    void foo(void) {
      chars b = make();
    }
    

    Or perhaps preferably in case of an ADT or similar, return a pointer to a malloc:ed object. Passing structs by value is usually not a good idea.

提交回复
热议问题