Thread: PROBLEM: Passing optional args to a function !!!

  1. #1
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43

    Exclamation PROBLEM: Passing optional args to a function !!!

    hello friends,

    Look at the snap of code below:

    Code:
    void
    set_color(GtkWidget *widget, int state, char* fg_color, char* bg_color = "")
    {
        GdkColor color_fg, color_bg;
        
        if(bg_color == "")
        {
            gdk_color_parse(fg_color, &color_fg);
            gtk_widget_modify_fg(widget, state, &color_fg);
        }
        else
        {
            gdk_color_parse(fg_color, &color_fg);
            gdk_color_parse(bg_color, &color_bg);
        
            gtk_widget_modify_fg(widget, state, &color_fg);
            gtk_widget_modify_bg(widget, state, &color_bg);    
        }
    }
    This code is giving me the error:
    Code:
    error: expected ‘;’, ‘,’ or ‘)’ before ‘=’ token.
    in the line:
    Code:
    set_color(GtkWidget *widget, int state, char* fg_color, char* bg_color = "")
    Can anyone help me please.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    C doesn't support optional ( better default ) parameters.
    Kurt

  3. #3
    Registered User Ravi Raj's Avatar
    Join Date
    May 2012
    Location
    INDIA
    Posts
    43
    Wow !
    the forum is so quick. I loved it.

    Thanks a lot Zuk. I will modify my code accordingly.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Ravi Raj View Post
    the forum is so quick. I loved it.
    Guess you were lucky. Kurt

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
        if(bg_color == "")
    This wouldn't work anyway. It is comparing pointers.
    Use strcmp() instead.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  2. Function problem - too few args
    By dynamethod in forum C++ Programming
    Replies: 8
    Last Post: 03-09-2008, 07:23 AM
  3. C optional function parameter?
    By fredanthony in forum C Programming
    Replies: 5
    Last Post: 01-08-2007, 04:06 PM
  4. Passing optional auto_ptr to function
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2006, 12:56 PM
  5. Passing Args Error
    By peking1983 in forum C Programming
    Replies: 4
    Last Post: 02-11-2003, 08:54 PM

Tags for this Thread