JDL Wahaha JDL Wahaha - 4 months ago 20
C Question

C parameter set but not used

I am using C to build a pebble watchface. I've been getting an error

parameter 'light_bitmap' set but not used [-Werror=unused-but-set-parameter]
. The variable is however used in my function. I'm not sure what this message mean. Below is the code that I have:

In header declaration:

void backlight_subscribe(GBitmap *light_bitmap, int start_hour, int end_hour);


In definition .c :

void backlight_subscribe(GBitmap *light_bitmap, int start_hour, int end_hour) {
int hour = get_current_hour();
if((hour >= start_hour) || (hour < end_hour)) {
accel_tap_service_subscribe(tap_handler);
light_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_LIGHT_ON_ICON);
} else {
accel_tap_service_unsubscribe();
light_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_LIGHT_OFF_ICON);
}
}


and in main.c:

static GBitmap *light_bitmap;
...
static void window_load(Window *window) {
...
backlight_subscribe(light_bitmap, 22, 8);
...
}

static void init(void) {
s_main_window = window_create();
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_stack_push(s_main_window, false);
}

...

int main(void) {
init();
app_event_loop();
deinit();
}


Full error message:

[16/28] c: src/accel_sensor.c -> build/src/accel_sensor.c.16.o
../src/accel_sensor.c: In function 'backlight_subscribe':
../src/accel_sensor.c:22:35: error: parameter 'light_bitmap' set but not used [-Werror=unused-but-set-parameter]
void backlight_subscribe(GBitmap *light_bitmap, int start_hour, int end_hour) {
^
cc1: all warnings being treated as errors
Waf: Leaving directory `/home/adminuser/pebble/pebble-app/watchface/build'
Build failed


Might anyone see where I'm doing wrong?

Answer

Just as the error says, in your backlight_subscribe function, you're assigning a value to light_bitmap but you're not reading from it. That means it's not being used.

In C, all parameters are pass by value. So the value you assign to light_bitmap is visible only in that function. If you're intending to change the variable you're passing in, you need to pass in the address of the variable in question, change the function to accept a pointer to that variable, then dereference the pointer when you assign to it:

void backlight_subscribe(GBitmap **light_bitmap, int start_hour, int end_hour) {
    int hour = get_current_hour();
    if((hour >= start_hour) || (hour < end_hour)) {
        accel_tap_service_subscribe(tap_handler);
        *light_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_LIGHT_ON_ICON);
    } else {
        accel_tap_service_unsubscribe();
        *light_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_LIGHT_OFF_ICON);
    }
}

Then you call the function like this:

backlight_subscribe(&light_bitmap, 22, 8);

As a side note, the function parameter light_bitmap is masking a global variable of the same name. That can cause confusion, so try to avoid that.