4

I was browsing a template (at cloudpebble.net) for building a Pebble watch face and came across this code:

void handle_minute_tick(AppContextRef ctx, PebbleTickEvent *t) {
  (void)t; // NOOP?
  (void)ctx; // NOOP?

  display_time(t->tick_time);
}


void handle_init(AppContextRef ctx) {
  (void)ctx; // NOOP?

  window_init(&window, "Big Time watch");
  window_stack_push(&window, true);
  window_set_background_color(&window, GColorBlack);

  resource_init_current_app(&APP_RESOURCES);

  // Avoids a blank screen on watch start.
  PblTm tick_time;

  get_time(&tick_time);
  display_time(&tick_time);
}


void handle_deinit(AppContextRef ctx) {
  (void)ctx; // NOOP?

  for (int i = 0; i < TOTAL_IMAGE_SLOTS; i++) {
    unload_digit_image_from_slot(i);
  }

}

What purpose do the lines I've indicated serve?

Cade Roux
  • 88,164
  • 40
  • 182
  • 265

1 Answers1

10

The only purpose of those lines is to silence compiler warnings like -Wunused-parameter. Note that those variables aren't used anywhere in the function body; casting them to void essentially tells the compiler that that's intentional.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • Note that another (better?) way to do this is to remove their names from the first line of the function, like: `void handle_minute_tick(AppContextRef, PebbleTickEvent *)` – StilesCrisis Apr 30 '13 at 01:23
  • 3
    @StilesCrisis, that's legal in C++, but not in C. See [this question](http://stackoverflow.com/questions/8776810/parameter-name-omitted-c-vs-c). – Cairnarvon Apr 30 '13 at 01:24
  • 3
    In this case the parameter `t` *is* actually used, so the `(void)t;` line appears to be a mistake. – caf Apr 30 '13 at 01:28
  • This code is C++. It is declaring variables in the middle of function bodies. – StilesCrisis Apr 30 '13 at 01:36
  • 1
    @StilesCrisis, C99 lets you do that too. – Cairnarvon Apr 30 '13 at 01:37
  • Is this a generally accepted idiom for suppressing unused parameters now? Seems like it would be better to use a pragma for that. – Cade Roux Apr 30 '13 at 06:34