#include #include // * // Meaning 1: int *x; // x is the address of an integer, not an int itself // // Note: x is 8 bytes, not 4 // Meaning 2: *x = 5; // The int x points to should be 5 // Meaning 3: x * 5; // * means multiplication here! // So how about this? // int x = *y * *z; // // & // Meaning 1: The address of, &x is the address of x // Meaning 2: x && y // Logical and // Meaning 3: x & y // Bitwise and // So how about this? // if(&x & &y) // // x[0] is the same as *x // x[1] is the same as *(x+1) // One important pitfall: int* x, y; // x is a pointer, y is NOT. y is an int. // I prefer this: int *z; // You can do this: int *a, *b; // Both are pointers void foo(int *A){ *A = 100; } int* print_array(int *A, size_t len){ printf("["); for(size_t i = 0; i < len; i++) printf("%d, ", A[i]); printf("\b\b]\n"); return A; } int* range(size_t end){ int *newarray = malloc(sizeof(int) * end); if(!newarray) // Error handling: malloc may return 0! return 0; for(int i = 0; i < end; i++) newarray[i] = i; return newarray; } int main(){ int x = 5; printf("%d\n", x); foo(&x); printf("%d\n", x); int numbers[10] = {0,1,2,3,4,5,6,7,8,9}; print_array(numbers, 10); // An array on the heap! int *other_numbers = malloc(10 * sizeof(int)); for(int i = 0; i < 10; i++) other_numbers[i] = i+100; print_array(other_numbers, 10); free(print_array(range(20), 20)); free(other_numbers); return 0; }