From a99549a180f3dcdfd2fa7d6fe94ee88e0ccb4139 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 20 Sep 2014 10:09:57 -0400 Subject: [PATCH] Split anonymous structs into two examples --- dynamic.c | 7 +++---- gsl_distance.c | 7 +++---- seamlessthree.c | 34 ++++++++++++++++++++++++++++++++++ seamlesstwo.c | 2 +- times_table.c | 1 - tutorial/divisions.c | 6 +++--- tutorial/do_while.c | 1 - tutorial/item_seven.c | 2 +- tutorial/pointer_in.c | 2 +- unicode.c | 2 ++ 10 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 seamlessthree.c diff --git a/dynamic.c b/dynamic.c index 997d0b1..84d604a 100644 --- a/dynamic.c +++ b/dynamic.c @@ -1,7 +1,6 @@ /* Compile with: LDLIBS="-lm -ldl -lreadline" CFLAGS="-g -Wall -std=gnu11 -O3" make dynamic */ -#define _GNU_SOURCE //cause stdio.h to include asprintf #include #include #include @@ -12,12 +11,12 @@ void get_a_function(){ fprintf(f, "#include \n" "double fn(double in){\n"); char *a_line = NULL; - char *header = ">>double fn(double in){\n>> "; + char *prompt = ">>double fn(double in){\n>> "; do { free(a_line); - a_line = readline(header); + a_line = readline(prompt); fprintf(f, "%s\n", a_line); - header = ">> "; + prompt = ">> "; } while (strcmp(a_line, "}")); fclose(f); } diff --git a/gsl_distance.c b/gsl_distance.c index 4e994ac..721568f 100644 --- a/gsl_distance.c +++ b/gsl_distance.c @@ -12,7 +12,7 @@ double one_dist(gsl_vector *v1, void *v2){ long double distance(apop_data *data, apop_model *model){ gsl_vector *target = model->parameters->vector; - return -apop_map_sum(data, .fn_vp=one_dist, .param=target, .part='r'); + return -apop_map_sum(data, .fn_vp=one_dist, .param=target); } apop_model *min_distance= &(apop_model){ @@ -25,9 +25,8 @@ int main(){ 2.9, 8.6, -1.3, 3.7, 2.9, 1.1); - Apop_model_add_group(min_distance, apop_mle, .method= APOP_SIMPLEX_NM, + Apop_model_add_group(min_distance, apop_mle, .method= "NM simplex", .tolerance=1e-5); - Apop_model_add_group(min_distance, apop_parts_wanted); - apop_model *est=apop_estimate(locations, min_distance); + apop_model *est = apop_estimate(locations, min_distance); apop_model_show(est); } diff --git a/seamlessthree.c b/seamlessthree.c new file mode 100644 index 0000000..03d6b8b --- /dev/null +++ b/seamlessthree.c @@ -0,0 +1,34 @@ +/* Compile with: +make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11" seamlessthree +*/ +#include +#include + +typedef struct point { + double x, y; +} point; + +typedef struct { + union { + struct { + double x, y; + }; + point p2; + }; + double z; +} threepoint; + +double length (point p){ + return sqrt(p.x*p.x + p.y*p.y); +} + +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)); + double xylength = length(p.p2); + printf("Its projection onto the XY plane is %g units from the origin\n", xylength); +} diff --git a/seamlesstwo.c b/seamlesstwo.c index 5484975..8b48d33 100644 --- a/seamlesstwo.c +++ b/seamlesstwo.c @@ -1,5 +1,5 @@ /* Compile with: -make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11 --ms-extensions" seamlesstwo +make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11 -fms-extensions" seamlesstwo */ #include #include diff --git a/times_table.c b/times_table.c index d50d6f9..ae5fba4 100644 --- a/times_table.c +++ b/times_table.c @@ -9,7 +9,6 @@ void matrix_cross_base(double *list1, double *list2){ int count1 = 0, count2 = 0; while (!isnan(list1[count1])) count1++; while (!isnan(list2[count2])) count2++; - if (!count1 || !count2) {printf("missing data."); return;} for (int i=0; i int main(){ - printf("3./5=%g\n", 3./5); - printf("3/5=%i\n", 3/5); - printf("3%%5=%i\n", 3%5); + printf("13./5=%g\n", 13./5); + printf("13/5=%i\n", 13/5); + printf("13%%5=%i\n", 13%5); } diff --git a/tutorial/do_while.c b/tutorial/do_while.c index c178f93..4edeeda 100644 --- a/tutorial/do_while.c +++ b/tutorial/do_while.c @@ -6,7 +6,6 @@ void loops(int max){ printf("Hello #%i\n", i); i++; } while (i < max); //Note the semicolon. - } int main(){ diff --git a/tutorial/item_seven.c b/tutorial/item_seven.c index 8bca9be..061b65c 100644 --- a/tutorial/item_seven.c +++ b/tutorial/item_seven.c @@ -7,6 +7,6 @@ int main(){ char string[len]; intlist[7] = 7; - snprintf(string, 20, "Item seven is %i.", intlist[7]); + snprintf(string, len, "Item seven is %i.", intlist[7]); printf("string says: <<%s>>\n", string); } diff --git a/tutorial/pointer_in.c b/tutorial/pointer_in.c index dcf9630..cf8c8fc 100644 --- a/tutorial/pointer_in.c +++ b/tutorial/pointer_in.c @@ -6,7 +6,7 @@ void double_in(int *in){ } int main(){ - int *x= malloc(sizeof(int)); + int x[1]; *x= 10; double_in(x); printf("x now points to %i.\n", *x); diff --git a/unicode.c b/unicode.c index f47a5c2..f675787 100644 --- a/unicode.c +++ b/unicode.c @@ -40,6 +40,7 @@ int main(int argc, char **argv){ char *ucs = localstring_to_utf8(string_from_file(argv[1])); Stopif(!ucs, return 1, "Exiting."); + FILE *out = fopen("uout.html", "w"); Stopif(!out, return 1, "Couldn't open uout.html for writing."); fprintf(out, "", strlen(ucs)); fprintf(out, "Here it is, with each space-delimited element on a line " "(with commentary on the first character):
"); + ok_array *spaced = ok_array_new(ucs, " \n"); for (int i=0; i< spaced->length; i++, (spaced->elements)++){ fprintf(out, "%s", *spaced->elements); -- GitLab