i am getting the error "warning: initialization makes pointer from integer without a cast [enabled by default]"

StackOverflow https://stackoverflow.com/questions/21148270

  •  28-09-2022
  •  | 
  •  

Question

i am getting the error because for gtk_entry it takes char type as input but i want to give it int type so first i declared int type then this int type is declared to char type and given to gtk_entry but i am getting error.

#include<gtk/gtk.h>


void static call(GtkWidget *widget,gpointer data)
{
g_print("\n%s\n",gtk_entry_get_text(GTK_ENTRY(data)));
gint answ=4;
const char* ans=answ; //here is the line having error
gtk_entry_set_text(GTK_ENTRY(data),ans);
gtk_editable_select_region(GTK_EDITABLE(data),0,-1);

}

int main(int agrc, char *agrv[])

{
gtk_init(&agrc,&agrv);
GtkWidget *entry,*window,*button,*hbox;

window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);

hbox=gtk_hbox_new(0,0);
gtk_container_add(GTK_CONTAINER(window),hbox);
button=gtk_button_new_with_mnemonic("write text");
entry=gtk_entry_new();
g_signal_connect(button,"clicked",G_CALLBACK(call),entry);
g_signal_connect(entry,"activate",G_CALLBACK(call),entry);



gtk_box_pack_start(GTK_BOX(hbox),button,0,0,0);

gtk_box_pack_start(GTK_BOX(hbox),entry,0,0,0);

gtk_widget_show_all(window);
gtk_main();
return 0;

}
Était-ce utile?

La solution

When you want to convert an integer to char so you can pass such integer as parameter, you will do:

#include <stdio.h>

    int main(void)
    {
       char c[4];
       int i=5;
       sprintf(c, "%d", i);
       printf("%s %d", c, i);
       return 0;
    }

You use sprintf to print your integer into a char variable, and then use it. There is a function itoa that is not standard but will help you too.

itoa is used like:

itoa(c, i, 10);

edit:

Your code I think working is:

gint x=4; 
char y[4]; 
sprintf(y, "%d", x);
gtk_entry_set_text(GTK_ENTRY(data),y); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top