相关疑难解决方法(0)

如何使用printf显示off_t,nlink_t,size_t和其他特殊类型?

在我的程序中,我统计他们想要的文件并发送数据.stat的字段struct都是特殊类型:

struct stat {
  dev_t     st_dev;     /* ID of device containing file */
  ino_t     st_ino;     /* inode number */
  mode_t    st_mode;    /* protection */
  nlink_t   st_nlink;   /* number of hard links */
  uid_t     st_uid;     /* user ID of owner */
  gid_t     st_gid;     /* group ID of owner */
  dev_t     st_rdev;    /* device ID (if special file) */
  off_t     st_size;    /* total size, in bytes */
  blksize_t st_blksize; /* blocksize for file system I/O */
  blkcnt_t  st_blocks;  /* number …
Run Code Online (Sandbox Code Playgroud)

c linux printf portability

27
推荐指数
1
解决办法
2万
查看次数

%ld 格式转换以实现可移植性

我有一个代码库,旨在在没有警告的情况下进行编译,并在多种架构上运行而不会出现任何故障,所有 x86:MSDOS、Windows 32 控制台模式、Windows 32 GUI 模式、Linux 32 和 Linux 64。

\n\n

在添加对 Linux 64 的支持之前,这很容易。几乎所有数据都被声明为intBYTE、WORD 和 DWORD 的类型定义:

\n\n
typedef unsigned char   BYTE;\ntypedef unsigned short  WORD;\ntypedef unsigned long   DWORD;\n
Run Code Online (Sandbox Code Playgroud)\n\n

添加 64 位 gcc 支持后,DWORD 需要进行一些调整才能保持 32 位值,因为它表示存储的数据:

\n\n
// to compile DWORDs as 32 bits on 64-bit machines:\n#if __x86_64__\n typedef unsigned int    DWORD;\n#else\n typedef unsigned long   DWORD;\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n\n

这在所有环境下都运行良好:

\n\n
DWORD   data;\nprintf ("%lu", data);\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,gcc -Wall现在抱怨格式转换:

\n\n
warning: format \xe2\x80\x98%ld\xe2\x80\x99 expects argument of …
Run Code Online (Sandbox Code Playgroud)

c++ x86 printf 16-bit 32bit-64bit

4
推荐指数
1
解决办法
1万
查看次数

在没有创建对象的情况下从指针中查找结构的正确大小?

对不起,如果标题令人困惑.这是我的结构:

struct l_list{
   int number;
   char *name;
   double value;
   struct l_list *next;
};

typedef struct l_list *PhoneBook;
Run Code Online (Sandbox Code Playgroud)

主功能:

int main(void){

   printf("%u\n", sizeof(struct l_list));

   PhoneBook person1;

   printf("%u\n", sizeof(*person1));
   printf("%u\n", sizeof(PhoneBook));

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

20
20
4

据我所知,PhoneBook它显示的是4个字节,因为它只是指针的大小,但是如何从?找到实际结构的大小typedef PhoneBook

c struct pointers sizeof

3
推荐指数
2
解决办法
3687
查看次数

在printf中使用#define?

我想要为应用程序ID使用某种常量(所以我可以在printf中使用它).

我有这个:

#define _APPID_ "Hello World!"
Run Code Online (Sandbox Code Playgroud)

然后是简单的printf,将其调用为%s(字符串).它说出来了:

simple.cpp:32: error: cannot convert ‘_IO_FILE*’ to ‘const char*’ for argument ‘1’ to ‘int printf(const char*, ...)’

我将使用什么来定义要在printf中使用的应用程序ID?我试过了:

static const char _APPID_[] = "Hello World"`
Run Code Online (Sandbox Code Playgroud)

但它没有用,我认为同样的错误.

c++ string c-preprocessor

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×2

c++ ×2

printf ×2

16-bit ×1

32bit-64bit ×1

c-preprocessor ×1

linux ×1

pointers ×1

portability ×1

sizeof ×1

string ×1

struct ×1

x86 ×1