Pergunta

It runs well, but if I delete the third line in main() "printf("(main)s: %s\n",s);

The output encounters a decode error: it print unrecognized chars instead of helo world.

Here is my code :

#include <stdio.h>
#include <stdarg.h>
void fun1(int i, ...){
    va_list arg_ptr; 
    char *s=NULL; 

    va_start(arg_ptr, i); 
    s=va_arg(arg_ptr, char*); 
    va_end(arg_ptr); 
    printf("address of s in fun1: %x\n",&(*s));
    printf("(fun1)s: %s\n",s); 
    return; 
}
void main(){
    char *s = "hello world";
    printf("        address of s: %x\n",&s);
    printf("(main)s: %s\n",s);
    fun1(4);
}

How can i fix this ?

Foi útil?

Solução

Seems you love UB.

  1. %x is not for printing pointers. Use %p (but only for printing void* or char*).
  2. You are taking the first var_arg_parameter, but you didn't pass one.
  3. Unless you are in a freestanding environment (microcontroller or such), you have a bad prototype for main. Use int main() or int 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? */
}

Outras dicas

Because you printf function says %x and not %s The %x is for unsigned hexadecimal integer and the %s is for a string of characters.

warning: format '%x' expects argument of type 'unsigned int', but argument 2 has type 'char**'

To print the address of your char array you could try copying the value of the array to an int variable.

Your problem is

printf("        address of s: %x\n",&s); ///< this line right here

The variable s is a char*, and &s is a char**, using %x you are implicitly casting your char** to int and will be printed in hex.

(ISO/IEC ISO/IEC 9899:1999 (E), §6.3.1.1) [...] If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.

I heard that you only have a problem when you remove the line: printf("(main)s: %s\n",s);.

So if your char** gets implicitly converted to int or uint and then gets presented in hex who knows what address are you looking at.

You can check everything about how to format printf here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top