Please enable JavaScript.
Coggle requires JavaScript to display documents.
C/C++ (Variables, Arrays and Pointers (Arrays - Passed into functions use…
C/C++
Variables, Arrays and Pointers
Arrays - Passed into functions use only the name. Eg. max(ages). Cannot be returned. However max(ages, retAges) works where retAges is filled in the function
-
2D Arrays - Passed into function as normal. In function, second dimension must be specified eg. max(ages[] [5]). Stored on stack in row major order
-
Pointers - *pvar1 = &var1. & means address of that variable. * notation defines a pointer which points to a memory address. Eg. scanf("hello, %s", *pvar1);
-
-
-
-
Malloc - Assigns a bunch of memory space and returns a pointer to it. Eg. *p = malloc(20*sizeof(int)). Free will free the space. Should always be done otherwise called a memory leak
C Basic Syntax
-
-
User input - scanf("%d", &n); This scans an input number, stores in n
-
Macro - #define MAX 100 or #define max(a, b) (return a)
-
Print - printf("Hello World, %s", args);
String Array - char[][100]; x size array, where each string is 100 chars.
-
User Defined Datatypes
-
-
Struct pointers - e = a1, *p, *a2; either p = a1 or a2 = malloc...
-
-
-
-
-
Enum - Eg. enum week {Mon, Tue, Wed, etc};
Theory
When defining a variable, if no value is stored, garbage value saved in memory location
Function Calls - Executed in the stack, for each call a section of stack is allocated (Stack Frame)
Stack overflow - Each recursive call creates new stack frames, if no stop, then stack runs out of space
-
-
-
Optimisation
Make small functions inline. Eg undo small functions as they require stack load/unload. Good alternative is macros
-
-
-
-
-