470,833 Members | 1,631 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

default parameter with ...

Hi
The following code have a compile error:
printf_format.cpp: In function `void print(int, char*, ...)':
printf_format.cpp:6: invalid conversion from `const char*' to `int'
printf_format.cpp: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("numberOfSpace=%d\n",numberOfSpace);
}

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

thanks
from Peter (cm****@hotmail.com)

Mar 8 '06 #1
2 1790
cm****@hotmail.com wrote:
Hi
The following code have a compile error:
printf_format.cpp: In function `void print(int, char*, ...)':
printf_format.cpp:6: invalid conversion from `const char*' to `int'
printf_format.cpp: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("numberOfSpace=%d\n",numberOfSpace); ITYM `printf' on the line above, as opposed to `print'. }

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


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
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Mar 8 '06 #2
cm****@hotmail.com wrote in news:1141790204.330131.278410
@e56g2000cwe.googlegroups.com:
Hi
The following code have a compile error:
printf_format.cpp: In function `void print(int, char*, ...)':
printf_format.cpp:6: invalid conversion from `const char*' to `int'
printf_format.cpp: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("numberOfSpace=%d\n",numberOfSpace);
}

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


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.
Mar 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Alex Panayotopoulos | last post: by
18 posts views Thread by Dan Cernat | last post: by
2 posts views Thread by Ekim | last post: by
2 posts views Thread by The Plankmeister | last post: by
7 posts views Thread by A.M | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.