1) Let color be the following structure: struct color { int red; int green; int
ID: 3713564 • Letter: 1
Question
1) Let color be the following structure:
struct color {
int red;
int green;
int blue;
}
write function for f-i.
f) void reset(struct color *p)
Reset the red, green and blue member to 0 of the color by using a pointer pointing to it.
g) Write a declaration for a const variable named MAGENTA of type struct color and initialize its members as 255,0, and 255, respectively.
h) Write statements to declare an array of colors using pointer and allocate memory space to it using malloc. The size of the array is 5.
i) Continue the question above and write statements to initialize the colors in the array. Each member in the color should be initialized as 0.
Explanation / Answer
(f)
void reset(struct color *p)
{
p->red = 0;
p->green = 0;
p->blue = 0;
}
(g)
const struct color MAGENTA;
MAGENTA.red = 255;
MAGENTA.green = 0;
MAGENTA.blue = 255;
(h)
struct color **arr = ( struct color ** )malloc( 5 * sizeof( struct color * ) );
int i;
for( i = 0 ; i < 5 ; i++ )
arr[i] = ( struct color * )malloc( 5 * sizeof( struct color ) );
(i)
for( i = 0 ; i < 5 ; i++ )
{
arr[i]->red = 0;
arr[i]->green = 0;
arr[i]->blue = 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.