Skip to content

Instantly share code, notes, and snippets.

@emiflake
Created March 10, 2019 20:37
Show Gist options
  • Save emiflake/542adedd5e122fdc7070acd06c4e29b4 to your computer and use it in GitHub Desktop.
Save emiflake/542adedd5e122fdc7070acd06c4e29b4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
/* this */
int* mk_arr_point(int x, int y)
{
int *p;
p = malloc(sizeof(int) * 2);
p[0] = x;
p[1] = y;
return (p);
}
/* vs */
struct s_point {
int x;
int y;
};
struct s_point* mk_s_point(int x, int y)
{
struct s_point* p;
p = malloc(sizeof(struct s_point));
p->x = x;
p->y = y;
return (p);
}
int main(void)
{
int* point1 = mk_arr_point(2, 5);
struct s_point* point2 = mk_s_point(2, 5);
printf("%d, %d\n", point1[0], point1[1]);
printf("%d, %d\n", point2->x, point2->y);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment