default parameter with ...

C

cmk128

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 ([email protected])
 
A

Artie Gold

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
 
A

Andre Kostur

(e-mail address removed) wrote in @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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top