Compile warning with gtk_entry_get_text

I have this function callback in my c program

 void on_filename_changed (GtkWidget *b)
  char* value;
  value = gtk_entry_get_text (GTK_ENTRY (b) );
  printf("filename changed to %s\n", value);
  printf("filename changed to %s\n", filename);

and I get this warning

biassup.c: In function ‘on_filename_changed’:
biassup.c:216:9: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
   value = gtk_entry_get_text (GTK_ENTRY (b) );

Any suggestion what I can do to get rid of the warning? The code seems to run fine, but I’m trying to clean up the compile-time warnings. This is with gtk-3.0



The gtk_entry_get_text() function returns a const char*—i.e. a string owned by the GtkEntry widget that you should not free yourself. This means that you need to declare value as a const char*.

Side note: don’t use strcpy(); you should either use strncpy(), if you know the size of the buffer, or (better) you should use a variable allocated on the heap and a function like g_strdup() to copy the string that you get out of GtkEntry.

Thanks, I thought of this last night:
const char* value = gtk_entry_get_text (GTK_ENTRY (b) );

which solves the problem.



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