Function Pointers and Arguments

This is a discussion on Function Pointers and Arguments within the C Programming forums, part of the General Programming Boards category; I'm using Gtk+ and when setting functions that listen for events a function pointer, and seperate set of data is ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Function Pointers and Arguments

    I'm using Gtk+ and when setting functions that listen for events a function pointer, and seperate set of data is passed to it. The function itself then as generic parameters. IE: GtkWidget *widget and gpointer data
    . A gpointer is basically the same as a void pointer and I guess the widget is the object the event was triggered on. I have functions working where I pass in one argument (an array of structs), but I want to pass in more (a string too). Heres my function. You can ignore most of the code, at the top I want to pack fname in with the data. The only way I can think of getting this to work would be to get the string length, string, and array of structs. malloc size to hold it all, copy the data to the allocated space, ten have to dissasemble the mess again inside the function. Is there an easier way to do this?
    Code:
    static void write_config(GtkWidget *widget, gpointer data)
    {
    	syntax *type = (syntax*) data;
    	const char* fname = "output.xml";
    	FILE *fp = fopen(fname, "w");
    	int i;
    	if(! fp)
    	{
    		printf("Error writing to file.\n");
    		return;
    	}
    
    	fprintf(fp, "<style-scheme id=\"output\" name=\"output\" version=\"1.0\">\n");
    	for(i=0; i<30; i++, type++)
    	{
    		// Get Foreground and background colors
    		GdkColor foreground, background;
    		GtkColorButton *fore = GTK_COLOR_BUTTON(type->fore);
    		GtkColorButton *back = GTK_COLOR_BUTTON(type->back); 
    		gtk_color_button_get_color(fore, &foreground);
    		gtk_color_button_get_color(back, &background);
    
    		// Get Text Type States
    		GtkToggleButton *bold_button = GTK_TOGGLE_BUTTON(type->bold);
    		GtkToggleButton *italic_button = GTK_TOGGLE_BUTTON(type->italic);
    		GtkToggleButton *ulined_button = GTK_TOGGLE_BUTTON(type->ulined);
    		gboolean is_bold = gtk_toggle_button_get_active(bold_button);
    		gboolean is_italic = gtk_toggle_button_get_active(italic_button);
    		gboolean is_ulined = gtk_toggle_button_get_active(ulined_button);
    		 
    		// Print Line To File
    		fprintf(fp, "\t<style name=\"%s\"\t\t", SYNTAX_OUTPUT[i]);
    		fprintf(fp, "foreground=\"#%s\" ", gdk_to_hex(foreground));
    		fprintf(fp, "background=\"#%s\" ", gdk_to_hex(background));
    		if(is_bold)   fprintf(fp, "bold=\"true\" ");  // <- Must test this works
    		if(is_italic) fprintf(fp, "italic=\"true\" ");
    		if(is_ulined) fprintf(fp, "underline=\"true\" ");
    		fprintf(fp, "/>\n");	
     	}
    	fprintf(fp, "</style-scheme>");
    	popup(NULL, "File Saved as output.xml");
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The simple way is to pass in a more complicated struct, e.g.
    Code:
    struct complex
    {
        syntax *type;
        char *fname; 
    };
    then
    Code:
    static void write_config(GtkWidget *widget, gpointer data)
    {
            struct complex *cptr = data;   // No need for a cast in C, C++ needs a cast. 
    	syntax *type = cptr->type;
    	const char* fname = cptr->fname;
    ....
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, that makes sense. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  2. Passing pointers to structs as function arguments
    By Da-Nuka in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2005, 01:10 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Pointers as function arguments
    By VanJay011379 in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:51 PM
  5. pointers as function arguments
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2002, 06:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21