34 lines
967 B
C
34 lines
967 B
C
/*
|
|
* HW 12, questions 1 to 3: measure struct A and struct B.
|
|
*
|
|
* Predict first (Canvas question 1), then finish main so it prints
|
|
* the size and every member offset of both structs.
|
|
*
|
|
* Build and run:
|
|
* gcc -std=c99 -pedantic -Wall -Wextra -o layout layout.c
|
|
* ./layout
|
|
*/
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
struct A { char a; int b; char c; double d; };
|
|
struct B { double d; int b; char a; char c; };
|
|
|
|
int main(void)
|
|
{
|
|
/*
|
|
* sizeof and offsetof both give a size_t, which printf prints with %zu.
|
|
* offsetof(struct A, b) is the number of bytes from the start of a
|
|
* struct A to its member b. The first line is done for you.
|
|
*/
|
|
printf("sizeof(struct A) = %zu\n", sizeof(struct A));
|
|
|
|
/*
|
|
* Your task, one line each, with these labels:
|
|
* "offsetof(struct A, a) = " and likewise for b, c, d
|
|
* "sizeof(struct B) = "
|
|
* "offsetof(struct B, d) = " and likewise for b, a, c
|
|
*/
|
|
|
|
return 0;
|
|
}
|