c - Why does a pointer work and not a normal variable in this code? -


in below code following warnings went built:

in function ‘on_btn_convert_clicked’:|

warning: assignment makes integer pointer without cast [enabled default]|

warning:passing argument 2 of ‘gtk_label_set_text’ makes pointer integer without cast [enabled default]|

||=== build finished: 0 error(s), 2 warning(s) (0 minute(s), 1 second(s)) ===|

if run program open gui button in question cause segmentation fault when pressed , crash program.

#include <stdlib.h> #include <stdio.h> //include gtk headers #include <gtk/gtk.h>  //define pointer variable names gtkwidget  *plblfilename; gtkwidget  *pbtnconvert; gtkwidget  *pbtnfilechooser;   //prototype functions char on_btn_convert_clicked();  char on_btn_convert_clicked() {    //define variables    char hello;     hello = "hello!";     gtk_label_set_text(gtk_label(plblfilename), hello);    return 0; }  //start main loop int main( int    argc,       char **argv ) {     gtkbuilder *builder;     gtkwidget  *window;     gerror     *error = null;      //init gtk+     gtk_init( &argc, &argv );      //create new gtkbuilder object     builder = gtk_builder_new();     //load ui file. if error occurs, report , quit application.     //replace "tut.glade" saved project.     if( ! gtk_builder_add_from_file( builder, "testgtk.ui", &error ) )     {         g_warning( "%s", error->message );         g_free( error );         return( 1 );     }      //get main window pointer ui     window = gtk_widget( gtk_builder_get_object( builder, "windowmain" ) );      // pointer label , button     plblfilename = gtk_widget(gtk_builder_get_object(builder, "lbl_filename"));     pbtnconvert = gtk_widget(gtk_builder_get_object(builder, "btn_convert"));     pbtnfilechooser = gtk_widget(gtk_builder_get_object(builder, "btn_choose"));      //connect button signal     g_signal_connect(g_object(pbtnconvert), "clicked", g_callback(on_btn_convert_clicked), null);      //destroy builder, since don't need anymore     g_object_unref( g_object( builder ) );      //show window. other widgets automatically shown gtkbuilder     gtk_widget_show( window );      //start main loop     gtk_main();      return( 0 ); } 

however, if make variable 'hello' pointer errors go away , works.

like this:

char on_btn_convert_clicked() {    //define variables    char *hello;     hello = "hello!";     gtk_label_set_text(gtk_label(plblfilename), hello);    return 0; } 

i dont understand how pointer, object points place in memory, can made equal "hello" , still work this?

can explain me why 'char hello' variable needs pointer (*) , cant string or 'char[]'?

finally, can summarize why working versus version normal char variable?

a constant string literal "hello" array of 6 characters ('h', 'e', 'l', 'l', 'o' , terminator '\0'). when use it, can decay pointer first element.

it's having actual array:

char hello_array[6] = { 'h', 'e', 'l', 'l', 'o', '\0' }; 

and assigning pointer like

char *hello = &hello_array[0]; 

the above happening when do

char *hello = "hello"; 

when have

char hello = "hello"; 

it's doing

char hello = &hello_array[0]; 

that doesn't work. it's same when calling function, expects argument of type char * , pass argument of type char. 2 types not same long stretch.


another way of explaining might think this: variable place store value. when have variable of type char can store single character. when have pointer character can store that, pointer character (actually memory address).

for example, lets have

char = 'a'; char *b = &a; 

somewhat graphically seen this:

                 +-----+ variable a: | 'a' |                 +-----+                 ^                 |                 +-----------------------+ variable b: | address of variable |                 +-----------------------+ 

the variable b points variable a.

the difference should quite obvious if print sizes of 2 types:

printf("sizeof(char) = %zu\n", sizeof(char)); printf("sizeof(char *) = %zu\n", sizeof(char *)); 

the first line should size of char 1 (it's specified in c specification 1 way). size of pointer should either 4 (on 32-bit system) or 8 (on 64-bit system).


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -