This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct date | |
{ | |
int day; | |
int month; | |
int year; | |
} | |
struct date my_bday; | |
struct date *bday_pointer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var params = [ "hello", true, 7 ] | |
var other = [ 1, 2, ...params ] | |
// [1, 2, "hello", true, 7] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// `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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |