title: How to communicate between callbacks in gtk+ c
body: <p>I'm working on a project right now, and I am trying to make 2 callbacks communicate with each other by passing to them the same variable (the callbacks share the same variable). Since the callbacks cannot return a value, I have passed a pointer to the value which is assigned in the main loop (main function).
  However, it isn't working at all! </p>
  
  <p>This is my code:</p>
  
  <pre><code>#include &lt;gtk/gtk.h&gt;
  
  static void inc_val (GtkWidget *widget, gpointer data)
  {
      int* value =  data;
      *value++;
      printf("value is: %d\n", *value);
  }
  
  static void inc_val_ten (GtkWidget *widget, gpointer data)
  {
      int* value =  data;
      *value+=10;
      printf("value is: %d\n", *value);
  }
  
  static void activate (GtkApplication *app, gpointer user_data)
  {
      GtkWidget *window;
      GtkWidget *grid;
      GtkWidget *button;
      int value = 0;
  
      window = gtk_application_window_new (app);
      gtk_window_set_title (GTK_WINDOW (window), "Window");
      gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  
      grid = gtk_grid_new ();
      gtk_container_add (GTK_CONTAINER (window), grid);
  
      button = gtk_button_new_with_label ("Add 1 to value");
      g_signal_connect (button, "clicked", G_CALLBACK (inc_val), &amp;value);
      gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
  
      button = gtk_button_new_with_label ("Add 10 to value");
      g_signal_connect (button, "clicked", G_CALLBACK (inc_val_ten), &amp;value);
      gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
  
      button = gtk_button_new_with_label ("Quit");
      g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
      gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
  
      gtk_widget_show_all (window);
  }
  
  int main (int argc, char **argv)
  {
      GtkApplication *app;
      int status;
  
      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);
  
      return status;
  }
  </code></pre>
  
  <p>The program has 3 buttons in the window:</p>
  
  <ol>
  <li>adding 1 to value</li>
  <li>adding 10 to value</li>
  <li>quitting the program</li>
  </ol>
  
  <p>value is an integer on the main loop. When one of the buttons (first or second) are pressed, the value getting bigger (by 1 or by ten) and the program prints the new value.</p>
  
  <p>I have run the program and clicked the buttons and that's what I've got:</p>
  
  <pre><code>value is: 46525384
  value is: 46525384
  value is: 46157330
  value is: -830528646
  value is: 56
  value is: 10 
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>value is: 1
  value is: 2
  value is: 12
  value is: 22
  value is: 23
  value is: 33
  </code></pre>
  
  <p>Anyone knows why does it happen? </p>
  
  <p>Thanks for helping!</p>
  
  <p><strong>Edit:</strong> I have tried the same thing, just instead of adding numbers to a value, I've tried to add button to the grid. Since the solution from the answer (under the question) worked at the previous code, I've made the grid global.</p>
  
  <p>So here is the new code:</p>
  
  <pre><code>#include &lt;gtk/gtk.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  
  GtkWidget *grid;
  
  static void add_button (GtkWidget *widget, gpointer data)
  {
      static int value = 0;
      GtkWidget* grid = data;
      value++;
      printf("attach to line %d\n", value);
      gtk_grid_attach(GTK_GRID(grid), gtk_button_new_from_stock(GTK_STOCK_NEW), 0, value, 1, 1);
  }
  
  static void activate (GtkApplication *app, gpointer user_data)
  {
      GtkWidget *window;
      GtkWidget *button;
  
      window = gtk_application_window_new (app);
      gtk_window_set_title (GTK_WINDOW (window), "Window");
      gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  
      grid = gtk_grid_new ();
      gtk_container_add (GTK_CONTAINER (window), grid);
  
      button = gtk_button_new_with_label ("Add Button");
      g_signal_connect (button, "clicked", G_CALLBACK (add_button), grid);
      gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
  
      gtk_widget_show_all (window);
  }
  
  int main (int argc, char **argv)
  {
      GtkApplication *app;
      int status;
  
      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);
  
      return status;
  }
  </code></pre>
  
  <p>However, it didn't work from some reason. Help will be appreciated!</p>