none
#define宏使用#符号 变成了 “(null)"的奇怪现象 RRS feed

  • 问题

  • 我在使用#define宏的时候出现了奇怪的现象 

    #define print_field(a,b) printf("0x%16X \t %s \n",a##.b ,#b)

    我想用这个输出一个结构体的某个域的值和相应域的名称

    但是却输出了   “0x               0       (null)” 的结果,0也是不对的值,但是并不总是出现,某些结构体输出正常

    下面是测试用的代码,从elf.h复制了一些结构体定义,vs2013 update4

    #include <stdio.h>
    #include <string.h>
    
    /* Standard ELF types.  */
    
    #include <stdint.h>
    
    /* Type for a 16-bit quantity.  */
    typedef uint16_t Elf32_Half;
    typedef uint16_t Elf64_Half;
    
    /* Types for signed and unsigned 32-bit quantities.  */
    typedef uint32_t Elf32_Word;
    typedef	int32_t  Elf32_Sword;
    typedef uint32_t Elf64_Word;
    typedef	int32_t  Elf64_Sword;
    
    /* Types for signed and unsigned 64-bit quantities.  */
    typedef uint64_t Elf32_Xword;
    typedef	int64_t  Elf32_Sxword;
    typedef uint64_t Elf64_Xword;
    typedef	int64_t  Elf64_Sxword;
    
    /* Type of addresses.  */
    typedef uint32_t Elf32_Addr;
    typedef uint64_t Elf64_Addr;
    
    /* Type of file offsets.  */
    typedef uint32_t Elf32_Off;
    typedef uint64_t Elf64_Off;
    
    /* Type for section indices, which are 16-bit quantities.  */
    typedef uint16_t Elf32_Section;
    typedef uint16_t Elf64_Section;
    
    /* Type for version symbol information.  */
    typedef Elf32_Half Elf32_Versym;
    typedef Elf64_Half Elf64_Versym;
    
    
    /* The ELF file header.  This appears at the start of every ELF file.  */
    
    #define EI_NIDENT (16)
    
    typedef struct
    {
    	unsigned char	e_ident[EI_NIDENT];	/* Magic number and other info */
    	Elf64_Half	e_type;			/* Object file type */
    	Elf64_Half	e_machine;		/* Architecture */
    	Elf64_Word	e_version;		/* Object file version */
    	Elf64_Addr	e_entry;		/* Entry point virtual address */
    	Elf64_Off	e_phoff;		/* Program header table file offset */
    	Elf64_Off	e_shoff;		/* Section header table file offset */
    	Elf64_Word	e_flags;		/* Processor-specific flags */
    	Elf64_Half	e_ehsize;		/* ELF header size in bytes */
    	Elf64_Half	e_phentsize;		/* Program header table entry size */
    	Elf64_Half	e_phnum;		/* Program header table entry count */
    	Elf64_Half	e_shentsize;		/* Section header table entry size */
    	Elf64_Half	e_shnum;		/* Section header table entry count */
    	Elf64_Half	e_shstrndx;		/* Section header string table index */
    } Elf64_Ehdr;
    
    
    #define print_field(a,b) printf("0x%16X \t %s \n",a##.b ,#b)
    
    int main()
    {
    
    	Elf64_Ehdr Ehdr = {1,2,3,4,5,6,7,8,9,0,1,2,3};
    	//???
    	//memset(&Ehdr, 1, sizeof(Ehdr));
    	print_field(Ehdr, e_type);
    	print_field(Ehdr, e_machine);
    	print_field(Ehdr, e_version);
    	print_field(Ehdr, e_entry);
    	print_field(Ehdr, e_phoff);
    	print_field(Ehdr, e_shoff);
    	print_field(Ehdr, e_flags);
    	print_field(Ehdr, e_ehsize);
    	print_field(Ehdr, e_phentsize);
    	print_field(Ehdr, e_phnum);
    	print_field(Ehdr, e_shentsize);
    	print_field(Ehdr, e_shnum);
    	print_field(Ehdr, e_shstrndx);
    	return 0;
    }


    2015年6月3日 10:09

全部回复

  • 你好,

    因为这个问题应该是VC++语言开发的问题,我帮你移到VC++开发论坛做更好的支持!

    谢谢你的合作!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年6月4日 6:48
  • #define get_field(a, b) (#a ## "." ## #b)
    #define print_field(a, b) printf("%s \t 0x%x \n", get_field(a, b), a.b)

    int main()
    {

    Elf64_Ehdr Ehdr = {1,2,3,4,5,6,7,8,9,0,1,2,3};
    //???
    //memset(&Ehdr, 1, sizeof(Ehdr));
    print_field(Ehdr, e_type);
    print_field(Ehdr, e_machine);
    print_field(Ehdr, e_version);
    print_field(Ehdr, e_entry);
    print_field(Ehdr, e_phoff);
    print_field(Ehdr, e_shoff);
    print_field(Ehdr, e_flags);
    print_field(Ehdr, e_ehsize);
    print_field(Ehdr, e_phentsize);
    print_field(Ehdr, e_phnum);
    print_field(Ehdr, e_shentsize);
    print_field(Ehdr, e_shnum);
    print_field(Ehdr, e_shstrndx);
    return 0;
    }

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2015年6月4日 9:54
    版主