[Subject: C Programming](Please answer all of the questions to get full points)
ID: 3765634 • Letter: #
Question
[Subject: C Programming](Please answer all of the questions to get full points)
1. a. Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double.
b. Use the complex tag to declare variables named c1, c2, and c3.
c. Write a function named make_complex that stores its two arguments(both of type double) in a complex structure, then returns the structure.
d. Write a function named add_complex that adds the corresponding members of its arguments(both complex structures), then returns the result(another complex structure).
2. Let color be the following structure
struct color{
int red; int green; int blue;
};
a. Write a declaration for a const variable named MAGENTA of type struct color whose members have the values 255,0, and 255 respectively.
b. Repeat part a, but use a designated initializer that doesn't specify the value of green, allowing it to default to 0.
3. Suppose that u is the following union:
union{
double a;
struct{
charb[4]; double c; int d;
} e;
char f[4];
}u;
If char values occupy one byte, int values occupy four bytes, and double values occupy eight bytes, how much space will a C compiler allocate for u?(Assume that the compiler leaves no "holes" between members.)
Explanation / Answer
1. a. Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double.
struct complex
{
double real;
double imaginary;
};
1. b. Use the complex tag to declare variables named c1, c2, and c3.
struct complex c1, c2, c3;
1. c. Write a function named make_complex that stores its two arguments(both of type double) in a complex structure, then returns the structure.
complex make_complex(double real, double imaginary)
{
complex c;
c.real=real;
c.imaginary=imaginary;
return c;
}
1. d. Write a function named add_complex that adds the corresponding members of its arguments(both complex structures), then returns the result(another complex structure).
complex add_complex(struct complex c1, struct complex c2)
{
struct complex c3;
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary;
return c3;
}
2. Let color be the following structure
struct color{
int red; int green; int blue;
};
2. a. Write a declaration for a const variable named MAGENTA of type struct color whose members have the values 255,0, and 255 respectively.
struct color MAGENTA;
MAGENTA.red=255;
MAGENTA.green=0;
MAGENTA.blue=255;
2. b. Repeat part a, but use a designated initializer that doesn't specify the value of green, allowing it to default to 0.
struct color{
int red; int green; int blue;
} MAGENTA = {
.green = 0,
};
3. Suppose that u is the following union:
union{
double a;
struct{
char b[4]; double c; int d;
} e;
char f[4];
}u;
If char values occupy one byte, int values occupy four bytes, and double values occupy eight bytes,
how much space will a C compiler allocate for u?(Assume that the compiler leaves no "holes" between members.)
Variable a occupied : 8 bytes of memory
Struct e:
Variable b occupied : 4 bytes of memory each char occupy 1 byte.
Variable c occupied : 8 bytes of memory
Variable d occupied : 4 bytes of memory
Variable e occupied : 24 bytes of memory
Variable f occupied : 4 bytes of memory each char occupy 1 byte.
Variable u occupied : 24 bytes of memory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.