Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 00:11 (UTC -07:00)
View GitHub Profile
@StevenJL
StevenJL / c_notes_array_pointers.c
Last active May 27, 2017 11:02
c_notes_array_pointers
int int_array[] = [1, 1, 2, 3, 5, 8, 13];
// To declare a pointer to an array, we simply go:
int *int_array_ptr;
// Note there is no need to say this is a pointer to
// an array, you just specify the type of element contained
// in the array. In that sense, this pointer could also
// be used to point to a regular integer.
// When you point the pointer to the array, you do not need to use
@StevenJL
StevenJL / c_notes_struct_pointers2.c
Created May 24, 2017 08:22
c_notes_struct_pointer_2
// a binary search tree can be implemented using structs with
// pointers to other structs as members.
struct bin_tree_node
{
int value;
struct bin_tree_node *left_child, *right_child;
};
@StevenJL
StevenJL / c_notes_struct_pointers.c
Created May 24, 2017 08:07
c_notes/pointers_to_structs
struct date
{
int day;
int month;
int year;
}
struct date my_bday;
struct date *bday_pointer;
@StevenJL
StevenJL / c_notes_const_pointer.c
Last active May 24, 2017 07:59
c_notes/pointers/const
char my_char = 'X';
char another_char = 'Y';
char * const char_fixed_ref = &my_char;
// char_fixed_ref should always point to my_char
char_fixed_ref = &another_char;
// this would raise a compiler error
const char *char_pointer_fixed_value = &my_char;
@StevenJL
StevenJL / element.jsx
Created March 28, 2017 05:45
react/element
// To render a React element into a root DOM node,
// pass both to ReactDOM.render():
const element = <h1>Hello, world</h1>;
ReactDOM.render(
element,
document.getElementById('root')
);
// Note that React elements are immutable and cannot
// be changed. The only way to change the UI is to
@StevenJL
StevenJL / spread.js
Created March 22, 2017 02:46
ES6/spread
var params = [ "hello", true, 7 ]
var other = [ 1, 2, ...params ]
// [1, 2, "hello", true, 7]
@StevenJL
StevenJL / anonfunc.js
Created March 22, 2017 02:36
ES6/arrowfunc
// Use the arrow notation to create anonymous functions
[1 , 3, 4, 5].map(v => v + 1)
evens = []
[1, 2, 3, 4, 5].forEach(v => {
if (v % 2 == 0)
evens.push(v)
})
@StevenJL
StevenJL / const.js
Last active March 22, 2017 02:17
ES6/constant
// `const` creates an 'immutable variable', that is a variables which cannot be
// re-assigned new content. Note this only makes the variable itself immutable,
// not its assigned content (for instance, in case the content is an object,
// this means the object itself can still be altered).
const PI = 3.14159;
// `let` creates a variable that is scoped to its nearest containing block.
// Compare this to `var` which scopes a variable to its nearest containing
// function.
let name = "Steve";
@StevenJL
StevenJL / bind.c
Created March 20, 2017 05:08
sock_programming/bind
int tcp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// AF_INET to create a IPv4 socket
// SOCK_STREAM to use a stream-based protocol
// IPPROTO_TCP to use the TCP protocol
// we create a struct to store ip address data;
struct sockaddr_in address_info;
// zero out the data in the struct
memset(&address_info, 0, sizeof(address_info));
@StevenJL
StevenJL / connect.c
Last active March 19, 2017 08:54
sock_programming/connect
int tcp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// AF_INET to create a IPv4 socket
// SOCK_STREAM to use a stream-based protocol
// IPPROTO_TCP to use the TCP protocol
// if tcp_sock is -1, the socket failed to create
if (tcp_sock < 0) {
fputs("socket creation failed", stderr);
exit(1);
}