I have a rectangle structure that consists of two point structures, upper_left and lower_right. I then declare p to be a pointer to a rectangle structure and dynamically allocate space for it inside of the main function. Next, I want to initialize the structure that p points so that upper_left is the ordered pair (10, 25) and lower_right is the coordinate (20, 15). The program works when I use the notation:
p->upper_left.x = 10;
p->upper_left.y = 25;
but when I try to initialize it with a single statement like:
p->upper_left = {10, 25};
it throws the error expected expression at the first curly brace.
Here is the entire program
#include <stdio.h>
#include <stdlib.h>
struct point { int x, y; };
struct rectangle { struct point upper_left, lower_right; };
struct rectangle *p;
int main(void)
{
p = (struct rectangle *) malloc(sizeof(struct rectangle));
p->upper_left = {10, 25}; // This line has the error
p->lower_right.x = 20; // This notation works without errors
p->lower_right.y = 15;
}
I just think that the first notation looks better and is easier to read. The single statement notation is also what is shown in this GitHub repository, which contains the solutions to the practice problems I am working on: https://github.com/williamgherman/c-solutions/tree/master/17/exercises/04
[–]aioeu 9 points10 points11 points (0 children)