Seems you love UB.
%x
is not for printing pointers. Use%p
(but only for printingvoid*
orchar*
).- You are taking the first var_arg_parameter, but you didn't pass one.
- Unless you are in a freestanding environment (microcontroller or such), you have a bad prototype for main. Use
int main()
orint main(int argc, char** argv)
.
In addition, you probably want to print s
resp. the param you passed, not where it's stored.
Added for clarification: UB means Undefined Behavior, an acronym any C / C++ programmer should know by heart. It literally means, "This program can exhibit any behavior whatsoever, including making demons fly out of your nose." What actually happens in any specific instance can be completely unpredictable and change at the drop of a hat.
#include <stdio.h>
#include <stdarg.h>
void fun1(int i, ...){
va_list arg_ptr;
char *s; /* removed superfluous initialisation */
va_start(arg_ptr, i);
s=va_arg(arg_ptr, char*);
va_end(arg_ptr);
printf("address of s in fun1: %p\n",(void*)&s);
printf("value of s in fun1: %p\n",s);
printf("address of string s: %p\n",s);
printf("value of string s: %s\n",s);
return;
}
int main(){
char *s = "hello world";
printf("address of s in main: %p\n",(void*)&s);
printf("value of s in main: %p\n",s);
printf("address of string s: %p\n",s);
printf("value of string s: %s\n",s);
fun1(4, s); /* fun1 will always access the first var-arg parameter as a string, so you must provide it */
return 0; /* can be omitted but only for main, so why do it? */
}