Skip to content

Instantly share code, notes, and snippets.

@yarcowang
Last active August 18, 2017 06:54
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 yarcowang/40364ac0fc1908104423def6f61e6a6e to your computer and use it in GitHub Desktop.
Save yarcowang/40364ac0fc1908104423def6f61e6a6e to your computer and use it in GitHub Desktop.
simple c code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[10];
} Person;
int main() {
// initialize to empty
Person* p = NULL;
// check if empty do memory allocated
if (!p) {
p = (Person*) malloc(sizeof(Person));
}
// set the name
strcpy(p->name, "yarco"); // the meaning is the same as `p->name = "yarco"`
printf("I'm %s\n", p->name);
// free memery
free(p);
return 0;
}
@yarcowang
Copy link
Author

yarcowang commented Aug 18, 2017

If remove types and memory management, the code is much more like this:

var p = null;
p.name = 'yarco';

in my mind...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment