user2467899
user2467899

Reputation: 575

return value of sizeof

I have this XDR struct:

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

And in my main:

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

I obtain in my txt file:

y.error's size: 0 bit

MORE:

I have created an XDR struct (struct Response) with rpcgen. I send this struct to a client with a socket:

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);

The problem is with xdr_Response function. So I think that the mistake is in y.error = 0

Upvotes: 0

Views: 358

Answers (1)

stepanbujnak
stepanbujnak

Reputation: 651

#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;
}

Works for me as expected. bool_t is not standard type so I typedef'd it. Also remember, on 64bit platform sizeof() returns unsigned long, so you need to use %ld in your fprintf function.

Upvotes: 1

Related Questions