Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active July 17, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harieamjari/ea363434874cddedc14d858927ed8567 to your computer and use it in GitHub Desktop.
Save harieamjari/ea363434874cddedc14d858927ed8567 to your computer and use it in GitHub Desktop.
New tricks learned in C
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct vec3D vec3D;
struct vec3D {
float x, y, z;
};
_Static_assert(sizeof(vec3D) == 12, "May not work");
vec3D *add_vec3D(vec3D *a, vec3D *b, int size) {
vec3D *ret = calloc(sizeof(vec3D), size);
assert(ret != NULL);
for (int i = 0; i < size; i++) {
ret[i].x += a[i].x + b[i].x;
ret[i].y += a[i].y + b[i].y;
ret[i].z += a[i].z + b[i].z;
}
return ret;
}
int main() {
const int len = 3;
vec3D *matrix3x3 = add_vec3D(
(vec3D[len]){
[0].x = 1,
[0].y = 2,
[0].z = 0,
[1].x = 0,
[1].y = 0,
[1].z = 0,
[2].x = 0,
[2].y = 0,
[2].z = 5,
},
(vec3D[len]){
[0].x = 1,
[0].y = 3,
[0].z = 0,
[1].x = 0,
[1].y = 0,
[1].z = 0,
[2].x = 0,
[2].y = 0,
[2].z = 2,
},
len);
for (int i = 0; i < len; i++) {
printf("%c %f %f %f\n", "xyz"[i], (&matrix3x3[0].x)[i],
(&matrix3x3[1].x)[i], (&matrix3x3[2].x)[i]);
}
free(matrix3x3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment