Pointers - Part 1 Flashcards
1 Pass By Value
Func(x, y)
func recieves copies of parameters
2 pass by reference
func(&x, &y)
func recieves address to parameter, can change original parameter value
3 Local Variable:
only accessible within a specific part of a program
4 Automatic Variable
allocates memory upon entering the variable’s block, and automatically un-allocates on exit. only activate/deactivates the memory.
main(){auto int a;} vs func(){int a;}
5 Static Variable
Preserves value even if out of scope. doesn’t need to reinitialize in case of new scope.
6 String in C
character sequence terminated with null character ‘\0’. stored as array of characters
char str[]=”bob”;
char str[4] = {‘b’, ‘o’, ‘b’, ‘\0’};
7 Pointer:
int *pointer = x
pointer == &x //address of x
*pointer == x //value of x
8 Dynamic Memory Allocation
Changing data structure size during runtime.
malloc(), calloc(), free(), realloc()
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
does not include local variables; they are assigned automatically by the program within its scope,
dynamic memory allocation is the programmer’s responsibility, including use of free() to avoid loss of data
9 Function Arguments
passed parameters to functions, either by value or by referece.
arguments are copied to the program stack at run time, then read by the function
10 Dangling Pointer
situation when pointer points to a variable that is out of scope, or the variable’s memory gets deallocated.
use static variables or assign NULL to the pointer to prevent errors
11 NULL
C NULL: special reserved pointer value that does not refer to any valid data object
12 Interface
collection of functions that describe the bahaviour of an object. program won’t know what to do with them yet, but you can define them later.
struct DrawableInterafce
{
void (* draw)( Object );
void (* rotate)( Object, double );
int (* get_heigth)( Object );
int (* get_width)( Object );
};
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/interface
13 Stack Variable
char *buff[500];
allocating data in stack is easy and fast, but limited, and deleted when you leave the scope. good for small local values.
local variables, arrays
14 Data Variable
containers for storing data values.
static, global variables
15 Heap “Variable”
char *buff = (char *)malloc(500);
memory used for global variables. supports dynamic memory allocation, but not tightly or automatically managed by the cpu. manually handled by the programmer, slower, and can resize variables (stack variables cannot be resized).
16 Array
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
collection of variables of the same type
17 Array Dynamic Allocation
int* ptr;
int n, i;
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(4 * sizeof(int));
for (i = 0; i < n; ++i) { ptr[i] = i + 1; }
18 Forward Declaration
void printThisInteger(int);
//code
void printThisInteger(int x) {
printf(“%d\n”, x);
}
declaration of a variable before providing a definition, but tell the compiler to permit use of the variable, but the definition would need to be provided somewhere.
Function1 printf
It is used to show output on the screen
printf(“Hello World!”);
Function2 scanf
It is used to take input from the user
scanf(“format_specifier”, &variables)
example-
int a;
scanf(“%d”,&a);
printf(“%d”,&a);
Function3 sprintf
char str[80];
sprintf(str, “Value of Pi = %f”, M_PI);
puts(str); //prints to terminal
sends formatted output to a string pointed to, by str.
Function4 assert
include <assert.h></assert.h>
void assert(int expression);
prints message to stderr and aborts the program if false or 0.
“Assertion failed: expression, file filename, line line-number.”
Function 5 malloc
Dynamic Memory Allocation for arrays.
ptr = (int*) malloc(100 * sizeof(int)); //will allocate 400 bytes of memory (100 * 4 bytes), pointer ptr holds the address of the first byte in the allocated memory.
Function 6 free
memory allocated with malloc() is not de-allocated automatically, so you need to run free() to remove deallocate memory.
free(ptr);