sizeof的返回值

时间:2013-07-07 15:41:13

标签: c linux sizeof

我有这个XDR结构:

struct Response {
    bool_t error;
    float result;
};
typedef struct Response Response;

在我的主要内容中:

Response y;
y.result = 5.7; 
y.error = 0; 
fprintf(f,"y.error's size: %d bit\n",sizeof(y.error));

我在我的txt文件中获取:

y.error's size: 0 bit

MORE:

我用rpcgen创建了一个XDR结构(struct Response)。我将此结构发送到带有套接字的客户端:

XDR xdrs_w;
Response y; 

FILE *stream_socket_w = fdopen(s, "w"); /* s is socket's file descriptor */
xdrstdio_create(&xdrs_w, stream_socket_w, XDR_ENCODE);

y.result = 6.8; 
y.error = 0; /* false */

if(!xdr_Response(&xdrs_w, &y)){
  printf("Error");
}

fflush(stream_socket_w);

问题在于xdr_Response功能。所以我认为错误在于y.error = 0

1 个答案:

答案 0 :(得分:1)

#include <stdio.h>

typedef unsigned char bool_t;

struct Response {
  bool_t error;
  float result;
};
typedef struct Response Response;

int main(int argc, char *argv[]) {
  Response r;

  r.result = 5.7;
  r.error = 0;

  printf("y.error's size: %zu bytes\n", sizeof(r.error));

  return 0;
}

按预期为我工作。 bool_t不是标准类型,所以我输入了它。还要记住,在64位平台上,sizeof()返回unsigned long,因此你需要在你的fprintf函数中使用%ld。

相关问题