Thread: How do I g_print a number using a function?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    30

    How do I g_print a number using a function?

    In the following code, how do I print the variable a (local to main function) using the print_number function?
    Code:
    #include <gtk/gtk.h>
    
    
    static void print_number (GtkWidget *widget, gpointer data) {
      g_print ("Number = %i\n",a);
    }
    
    
    
    
    
    
    int main (int   argc,char *argv[]) {
    
    
    //Declare and assign a value to variable a
      int a=15;	
    	
      GtkBuilder *builder;
      GObject *window;
      GObject *button;
      GObject *GTKMenuItem;
    
    
      gtk_init (&argc, &argv);
    
    
      /* Construct a GtkBuilder instance and load our UI description */
      builder = gtk_builder_new ();
      gtk_builder_add_from_file (builder, "demo.ui", NULL);
    
    
      /* Connect signal handlers to the constructed widgets. */
      window = gtk_builder_get_object (builder, "main_window");
      g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    
    
      button = gtk_builder_get_object (builder, "start_button");
      g_signal_connect (button, "clicked", G_CALLBACK (print_number), NULL);
    
    
      GTKMenuItem = gtk_builder_get_object (builder, "file_quit");
      g_signal_connect (GTKMenuItem, "activate", G_CALLBACK (gtk_main_quit), NULL);
    
    
      gtk_main ();
    
    
      return 0;
    }
    I get the error: ‘a’ undeclared (first use in this function. I understand why I get this error, since when I call the function in G_CALLBACK (print_number), I am not passing any arguments to it. So I tried this:

    Code:
    #include <gtk/gtk.h>
    
    
    static void print_number (GtkWidget *widget, gpointer data, int a) {
      g_print ("Number = %i\n",a);
    }
    
    
    int main (int   argc,char *argv[]) {
    
    
    //Declare and assign a value to variable a
      int a=15;	
    	
      GtkBuilder *builder;
      GObject *window;
      GObject *button;
      GObject *GTKMenuItem;
    
    
      gtk_init (&argc, &argv);
    
    
      /* Construct a GtkBuilder instance and load our UI description */
      builder = gtk_builder_new ();
      gtk_builder_add_from_file (builder, "demo.ui", NULL);
    
    
      /* Connect signal handlers to the constructed widgets. */
      window = gtk_builder_get_object (builder, "main_window");
      g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    
    
      button = gtk_builder_get_object (builder, "start_button");
      g_signal_connect (button, "clicked", G_CALLBACK (print_number(a)), NULL);
    
    
      GTKMenuItem = gtk_builder_get_object (builder, "file_quit");
      g_signal_connect (GTKMenuItem, "activate", G_CALLBACK (gtk_main_quit), NULL);
    
    
      gtk_main ();
    
    
      return 0;
    }
    and I get the error: too few arguments to function ‘print_number’. How do I pass the value of a into the function print_number?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The way you have it you are simply calling print_number directly (with the wrong number of parameters). Instead, you need to pass the data to the gpointer argument by passing it's address as the last argument of g_signal_connect. You can't just add parameters to the callback function. It can only have the two allowed parameters.
    Code:
    static void print_number(GtkWidget *widget, gpointer data) {
      g_print("Number = %d\n", *(int*)data);
    }
    
    // in main
      g_signal_connect(button, "clicked", G_CALLBACK (print_number), &a);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Number Function
    By askinne2 in forum C Programming
    Replies: 12
    Last Post: 10-30-2010, 08:23 PM
  2. Prime Number Function
    By mrcg in forum C Programming
    Replies: 10
    Last Post: 10-21-2009, 12:33 AM
  3. help with a number counting function
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 05:45 AM
  4. is a number? C function
    By guillermoh in forum C Programming
    Replies: 11
    Last Post: 02-04-2008, 11:42 AM

Tags for this Thread