default parameter with ...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmk128@hotmail.com

    #1

    default parameter with ...

    Hi
    The following code have a compile error:
    printf_format.c pp: In function `void print(int, char*, ...)':
    printf_format.c pp:6: invalid conversion from `const char*' to `int'
    printf_format.c pp:6: invalid conversion from `int' to `char*'

    I am using gcc 3.4, please HELP

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>

    void print (int numberOfSpace=0 ,char *args="", ...){
    print("numberOf Space=%d\n",num berOfSpace);
    }

    int main(){
    printf("asdad") ;
    return 0;
    }

    thanks
    from Peter (cmk128@hotmail .com)

  • Artie Gold

    #2
    Re: default parameter with ...

    cmk128@hotmail. com wrote:[color=blue]
    > Hi
    > The following code have a compile error:
    > printf_format.c pp: In function `void print(int, char*, ...)':
    > printf_format.c pp:6: invalid conversion from `const char*' to `int'
    > printf_format.c pp:6: invalid conversion from `int' to `char*'
    >
    > I am using gcc 3.4, please HELP
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <stdarg.h>
    >
    > void print (int numberOfSpace=0 ,char *args="", ...){
    > print("numberOf Space=%d\n",num berOfSpace);[/color]
    ITYM `printf' on the line above, as opposed to `print'.[color=blue]
    > }
    >
    > int main(){
    > printf("asdad") ;
    > return 0;
    > }
    >[/color]

    I'll make no other comments (though there are certainly some to be
    made). I'll leave it at the obvious typo.


    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

    "You can't KISS* unless you MISS**"
    [*-Keep it simple, stupid. **-Make it simple, stupid.]

    Comment

    • Andre Kostur

      #3
      Re: default parameter with ...

      cmk128@hotmail. com wrote in news:1141790204 .330131.278410
      @e56g2000cwe.go oglegroups.com:
      [color=blue]
      > Hi
      > The following code have a compile error:
      > printf_format.c pp: In function `void print(int, char*, ...)':
      > printf_format.c pp:6: invalid conversion from `const char*' to `int'
      > printf_format.c pp:6: invalid conversion from `int' to `char*'
      >
      > I am using gcc 3.4, please HELP
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      > #include <stdarg.h>
      >
      > void print (int numberOfSpace=0 ,char *args="", ...){
      > print("numberOf Space=%d\n",num berOfSpace);
      > }
      >
      > int main(){
      > printf("asdad") ;
      > return 0;
      > }[/color]

      gcc is right. The first parameter to your function is an int. You're
      passing it a const char *. Recall that if you want to change what you're
      passing for the 2nd parameter (and want the default value for the 1st),
      you still must specify the 1st paramter (so you'd have to:
      "print(0, "%s")"). Same idea goes for whatever parameters you're passing
      through your ellipsis. In order for any values to go to the ellipsis,
      you must specify the int and char* first.

      Comment

      Working...