C typedef of pointer to structure

后端 未结 6 867
暖寄归人
暖寄归人 2020-11-27 13:52

I had come across the following code:

typedef struct {
        double x;
        double y;
        double z;
} *vector;

Is this a v

6条回答
  •  失恋的感觉
    2020-11-27 14:19

    Yes it is. But it is imho bad style. Not the direct declaration of the struct, but the direct declaration of a pointer type. It is obfuscation, the information that a given variable or parameter is a pointer (and to a lesser extent for arrays) is extremly important when you want to read code.

    When reviewing code it is often difficult to see at first glance which function could have a side effect or not. If the types used hide this information, it adds a memorisation burden to the reader.

    int do_fancy(vector a, vector b); 
    

    or

    int do_fancy(vector *a, vector *b);
    

    in the first case I can miss easily that that function may change the content of a or b. In the second I'm warned.

    And when actually writing code I also know directly to write a->x and not have the compiler tell me error: request for memberx' in something not a structure or union`.

    I know, it looks like a personal taste thing, but having worked with a lot of external code, I can assure you that it's extremely annoying when you do not recognize the indirection level of variables. That's one reason I also dislike C++ references (in Java it's not because all objects are passed by reference, it's consistent) and Microsoft's LPCSTR kind of types.

提交回复
热议问题