Typedef redefinition with different types (‘uint8_t’ (aka ‘unsigned char’) vs ‘enum clockid_t’)

When you encounter a “typedef redefinition with different types” error in C or C++, it means that you are attempting to redefine a previously defined typedef with a different type.

Let’s understand this with an example. Assume that you have the following code:

    
      typedef unsigned char uint8_t;
      typedef enum { CLOCK_REALTIME, CLOCK_MONOTONIC } clockid_t;
      typedef unsigned int uint8_t; // redefinition error, different types
    
  

In this example, the first two typedefs are valid. However, the third typedef tries to redefine uint8_t as an unsigned int instead of an unsigned char. This leads to a redefinition error because it is not allowed to redefine a typedef with a different type.

To fix this error, you need to ensure that the typedef redefinitions have consistent types. In the above example, you can correct the code as:

    
      typedef unsigned char uint8_t;
      typedef enum { CLOCK_REALTIME, CLOCK_MONOTONIC } clockid_t;
      typedef unsigned char uint16_t;
    
  

Here, the third typedef has been corrected to redefine uint16_t as an unsigned char, which is consistent with the initial definition of uint8_t.

Read more

Leave a comment