Browse Source

added example of extending a structure with nested naynonymous declarations

master
T. Meissner 10 years ago
parent
commit
91bdbb65e2
2 changed files with 37 additions and 1 deletions
  1. +5
    -1
      21st_century_c/Makefile
  2. +32
    -0
      21st_century_c/seamlessone.c

+ 5
- 1
21st_century_c/Makefile View File

@ -1,4 +1,4 @@
CFLAGS = --std=c11 -Wall -O3
CFLAGS = -Wall -O3 --std=c11
boxes : boxes.c boxes : boxes.c
@ -10,6 +10,9 @@ show_tree : process_dir.h process_dir.c show_tree.c
simple_cplx : cplx.h complex.c simple_cplx.c simple_cplx : cplx.h complex.c simple_cplx.c
$(CC) `pkg-config --cflags --libs gsl` $(CFLAGS) complex.c $@.c -o $@ $(CC) `pkg-config --cflags --libs gsl` $(CFLAGS) complex.c $@.c -o $@
seamlessone : seamlessone.c
$(CC) $(CFLAGS) -fms-extensions -Wno-microsoft $@.c -o $@
.PHONY : all .PHONY : all
all : boxes show_tree simple_cplx all : boxes show_tree simple_cplx
@ -22,4 +25,5 @@ clean :
rm -f boxes rm -f boxes
rm -f show_tree rm -f show_tree
rm -f simple_cplx rm -f simple_cplx
rm -f seamlessone
rm -rf *.dSYM rm -rf *.dSYM

+ 32
- 0
21st_century_c/seamlessone.c View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
typedef struct point {
double x, y;
} point;
/*
Nested anonymous struct declaration compiles with MS extensions only.
So you have to use -fms-extensions flag with gcc / clang.
C11 standard allows no typedef in the nested anonymous declaration :(
*/
typedef struct {
struct point;
double z;
} threepoint;
double threelength(threepoint p) {
return sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
}
int main () {
threepoint p = {.x = 3, .y = 0, .z = 4};
printf("p is %g units from the origin\n", threelength(p));
}

Loading…
Cancel
Save