By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

default parameter with ...

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.