Warning: 'typedef' was ignored in this declaration

Hi,

I'm trying to declare a typedef button variables struct for tft touch code.

And I've put this code:

typedef struct button{
  uint16_t x;
  uint16_t y;
  uint8_t w;
  uint8_t h;
  uint16_t color;
};

button b1;

and got this warning:

buttons.ino:7:1: warning: 'typedef' was ignored in this declaration
 typedef struct button{
 ^~~~~~~

What should I do ?

C++ does not require the use of typedef when declaring a struct, hence the warning

1 Like

Is this how it should be done ?


struct button{
  uint16_t x;
  uint16_t y;
  uint8_t w;
  uint8_t h;
  uint16_t color;
};

button b1;

void initialize_button(struct button *b, uint16_t button_x, uint16_t button_y, uint8_t button_w, uint8_t button_h, uint16_t button_color){
  b->x = button_x;
  b->y = button_y;
  b->w = button_w;
  b->h = button_h;
  b->color = button_color;
}

Also which is better to use a pointer or non-pointer for argument in void initialize_button(struct button *b, ... ) ?

this is fine

struct button{
  uint16_t x;
  uint16_t y;
  uint8_t w;
  uint8_t h;
  uint16_t color;
};

button b1;

In c++ you could decide to pass either by a pointer as you did (without the struct)

void initialize_button(button *b, uint16_t button_x, uint16_t button_y, uint8_t button_w, uint8_t button_h, uint16_t button_color){

the call would be initialize_button(&b1, ....);

or by reference then it would be

void initialize_button(button &b, uint16_t button_x, uint16_t button_y, uint8_t button_w, uint8_t button_h, uint16_t button_color){
  b.x = button_x;
  b.y = button_y;
  b.w = button_w;
  b.h = button_h;
  b.color = button_color;
}

the call would be initialize_button(b1, ....);

PS:
in Arduino world camelCase is also the standard notation so your function could be named initializeButton(..) and struct types are usually denoted with a capital letter so struct Button {...

1 Like

No, initialization of the data members is the job of the constructor, not some separate function.
In this case you don't even need a constructor, just use aggregate initialization.

Even if you do need to pass the struct to a function, pass it by value or by reference. Only pass by pointer if it can be null. In this example, you could just return by value, no need for the cumbersome pointer output parameter.

You should not repeat the struct every time you use the struct type.

1 Like

of course in C++ you would go all the way and write the constructor as @PieterP said

1 Like

I remembered vaguely being plagued by this. In 'C', you have to repeat the 'struct' qualifier.

The typedef was just missing the name for the newly introduced type.

# include <stdio.h>
# include <stdlib.h>

typedef struct button {
  unsigned int x;
  unsigned int y;
  unsigned int color;
} myButtonType;

myButtonType b2;  // using the typedef

struct button b1; // you can say 'struct' if you want

button b3;        // but you have to if it's just C.

int main() {
  b1.color = 42;
  b2.x = 314;
  b3.y = 272;

  printf("\n%d\n", b2.x);
}

a7

1 Like

There's nothing stopping you from using the same name for the struct tag and the typedef:

typedef struct button {
  unsigned int x;
  unsigned int y;
  unsigned int color;
} button;

That's valid in both C and C++, and you can use either struct button or button (though I would capitalize it as Button in both cases, as J-M-L mentioned in Warning: 'typedef' was ignored in this declaration - #4 by J-M-L, to make it clear that it's a type rather than an object). You could even leave out the struct tag so you can use it only as button and not as struct button.

2 Likes

Thank you so much man for these details :slight_smile:

Yes, I'm noticing different methods for naming in different libraries; some in C and some in C++.

I mostly find both camelCase and all lower case with underscore for spacing. But I believe the camel case is the most one in Arduino world. The other one can be found in lower level C libraries if I'm correct here.

Yes camelCase is more recent

1 Like

Yeah, but I don't know it the main principles of declaring and defining structs in C and C++ are the same. In your example, the variable name will be the only instantiated variable of the struct, so I wanted to instantiate another struct variable then I either set it with the first variable or set it later elsewhere; like:

struct GROUPS{
    uint16_t a1;
    uint16_t a2;
} GROUP1, GROUP2;    // <-- declaring two variables of GROUPS struct

struct GROUPS GROUPA;   // <-- declaring a variable GROUPA of GROUPS struct
struct GROUPS GROUPB;   // <-- declaring a variable GROUPB of GROUPS struct

I was a little bit away of using structs so I forgot how to use the struct tag and the variable. And which one is the most important. I went back to my C compiler and did couple tests:

CASE 1: not using typedefs

  1. Tag name is important for declaring other variables.
  2. Not using a tag name is ok, but can't declare other variables.

CASE 2: using typedefs

  1. Using tag name is not important for declaring other variables. But the variable name is now the type name so I must use it to declare variables.
  2. Not using tag name is ok, but the variable name is a must to declare variables.

Hope my conclusion is correct.

Or just


struct GROUPS{
    uint16_t a1;
    uint16_t a2;
} GROUP1, GROUP2;    // <-- declaring two variables of GROUPS struct

GROUPS GROUPA;   // <-- declaring a variable GROUPA of type GROUPS
GROUPS GROUPB;   // <-- declaring a variable GROUPB of type GROUPS

1 Like

This will work fine in C++, but in C it's different, my post #11 is about my C test.

But the C++ version is more flexible.

The compiler we use with the IDE is C++ indeed
(Yes this is different in C)

1 Like

In my example I didn't instantiate any variables. button is both a struct tag (use it as struct button in C or C++ or as just button in C++) and a typedef (use it as button in C or C++).

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.