Is the below code good? Especially the memcpy to copy struct A on to struct B? Is this guaranteed to work across platforms (Linux, Mac, Windows GCC/MsVC/Clang compilers etc.)?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct A {
int a;
int b;
char c;
char s[12];
unsigned long long l;
};
struct B {
int a;
int b;
char c;
};
static void dumpA(struct A* a);
static void dumpB(struct B* b);
static void dumpA(struct A* a)
{
if (a != NULL) {
printf(" %4d %4d %4c %s\n", a->a, a->b, a->c, a->s);
}
}
static void dumpB(struct B* b)
{
if (b != NULL) {
printf(" %4d %4d %4c\n", b->a, b->b, b->c);
}
}
int main()
{
struct A a;
struct B b;
a.a = 10;
a.b = -2;
a.c = 'R';
strncpy(a.s, "Foo", strlen("Foo"));
memcpy(&b, &a, sizeof(struct B));
dumpA(&a);
dumpB(&b);
return 0;
}