Not known implementation of typedef in C

90 views Asked by At

I am confused about this example:

typedef int32_t voltage_dc_estimate_t[1];

Everything is OK but that [1] at the end of the type definition confuse me. Could someone please help me to understand that situation?

3

There are 3 answers

1
Arial On BEST ANSWER

To understand what's going on, you have to breakdown the code part by part

typedef int32_t voltage_dc_estimate_t[1];

typedef is declaring a new type called voltage_dc_estimate_t, which is an array of int32_t of size 1.

Note that while this logical sense, it is a very bad idea to do this, because you are better off just doing

typedef int32_t voltage_dc_estimate_t;

if you are only trying to save 1 element.

0
Yu Hao On

[1] means an array of 1 element.

voltage_dc_estimate_t is a type of an array of 1 element of type int32_t.

0
M.M On

To understand a typedef declaration, first understand what the declaration would be without it:

int32_t voltage_dc_estimate_t[1];

That would declare an array of uint32_t of length 1, named voltage_dc_estimate_t.

The effect of typedef is that the name voltage_dc_estimate_t will represent the type of that variable instead of being an actual variable. So in your example it means an array of int32_t of length 1.