CS计算机代考程序代写 #include

#include
#include

// an anonymous struct that also declares the variable a;
struct {
int i;
int j;
int k;
} *a;

// a simple struct
struct A {
int i;
int j;
}; // this semicolon is required

struct A* b;

// a complex struct

struct B {
int i[10];
struct A* a;
};

struct C {
int i;
struct B b;
int j;
};

struct C c;

// some structs to look at sizeof and alignment
struct D {
char c;
};

struct E {
char c;
int i;
};

struct F {
int i;
char c;
};

struct F f[10];

// *** HOME WORK *** … how are the variables w, x, y, and z different?
struct A w[10];
struct A* x;
struct A* y[10];
struct A** z;

int main (int argc, char** argv) {

// this is wrong, because sizeof(a) is size of pointer; i.e., 4 or 8 bytes
a = malloc(sizeof(a));

// this is correct; you can take sizeof *a without dereferencing it
a = malloc(sizeof(*a));

// this does not give you the byte-offset to j in struct A
printf(“%ld\n”, &b->j – &b->i);

// this does give you the byte offset to j in struct A
printf(“%ld\n”, ((char*)&b->j) – ((char*)&b->i));

// the offset to b.j is 56, not 52, because of alignment of a in struct B
printf(“%ld\n”, ((char*)&c.j) – ((char*)&c));

// the size of struct F must be 8 so that, for example &f[1].i is aligned
printf (“%ld %ld %ld %ld\n”, sizeof(struct D), sizeof(struct E), sizeof(struct F), ((char*)&f[1].i) – ((char*)&f[0].i));

// ********(********
// *** HOME WORK ***
// What is the difference between g1=g0 and gp1=gp0?
struct G {
int i, j;
};
struct G g0, g1, *gp0, *gp1;

g0.i=1;
g0.j=2;
g1=g0;

gp0 = malloc(sizeof(*gp0));
gp0->i=1;
gp0->j=2;
gp1=gp0;

printf(“(%d %d) == (%d %d)\n”, g0.i, g1.i, gp0->i, gp1->i);

g0.i = 3;
gp0->i = 3;

printf(“(%d %d) == (%d %d)\n”, g0.i, g1.i, gp0->i, gp1->i);
}