How to use anonymous struct in C

1 Answer

0 votes
#include <stdio.h>

typedef struct {
	struct { // anonymous struct
		float x, y;
	} pos;
	struct { // anonymous struct
		float r, g, b;
	} color;
} Vertex;

int main() {
    Vertex vr;
      
    printf("size: %zu\n", sizeof(vr));
    
    vr.pos.x = 100.0f;
    vr.pos.y = 200.0f;
  
    printf("vr.pos.x: %.2f\n", vr.pos.x);
    printf("vr.pos.y: %.2f\n", vr.pos.y);
    
    vr.color.r = 60.0f;
    vr.color.g = 255.0f;
    vr.color.b = 80.0f;
    
    printf("vr.color.r: %.2f\n", vr.color.r);
    printf("vr.color.g: %.2f\n", vr.color.g);
    printf("vr.color.b: %.2f\n", vr.color.b);
  
    return 0;
}

  

/*
run:
  
size: 20
vr.pos.x: 100.00
vr.pos.y: 200.00
vr.color.r: 60.00
vr.color.g: 255.00
vr.color.b: 80.00

*/

 



answered Nov 22, 2024 by avibootz
edited Nov 22, 2024 by avibootz

Related questions

1 answer 261 views
261 views asked Dec 27, 2020 by avibootz
1 answer 227 views
227 views asked Mar 17, 2018 by avibootz
1 answer 295 views
1 answer 230 views
230 views asked Mar 17, 2018 by avibootz
1 answer 318 views
1 answer 263 views
263 views asked Mar 17, 2018 by avibootz
...