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);
Q1 What is the ILRD convention for function names?
TitleCase, AKA PascalCase
Q2 What is the difference between using assert and if on parameters
When if tests false, it either doesn’t run the following block or proceeds to the else block.
Assert will abort the program and print a formatted STDERR
assert(1<0);
Q3 What are the escape characters for printf? For scanf?
alert (beep) \a
backslash
backspace \b
carriage return \r
double quote "
formfeed \f
horizontal tab \t
newline \n
null character \0
single quote '
vertical tab \v
question mark \?
\a Beeps the speaker
\b Backspace (moves the cursor back, no erase)
\f Form feed (ejects printer page; may clear the screen on some computers)
\n Newline, like pressing the Enter key
\r Carriage return (moves the cursor to the beginning of the line)
\t Tab
\v Vertical tab (moves the cursor down a line)
\ The backslash character
' The apostrophe
" The double-quote character
\? The question mark
\0 The “null” byte (that’s 0, not the letter O)
\Onn A character value in octal (base 8)
\xnnn A character value in hexadecimal (base 16)
Q4 What is a 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
Q5 What will happen if you use a dangling pointer?
you cause a segmentation fault (use free)
also create a core dump file, yay
Q6 a What will the following snippet print? b. What will happen if you do not cast ( (unsigned int*)fp; )?
a: type promotion is going to look… weird.
b: 3incompatible type, can’t cast to begin with.
Q7 What will the following code print?
33 apparently
Q8 What will the following code print?
3
Q9 Explain the following:
arr[ ] at location i (3)
array is already a pointer to the address of the first element. dereferencing the array’s address and adding to it will move through the array, and dereferencing the pointers will reveal the elements.
it’s funky, but i[arr] equates to i[int* arr], which equates to *(arr + i)
Q10
size 10, int array (or int pointers I suppose..)
character array, size 2
character array of size 2
character array of size 2
character array of size 2?
b.
str1 //lValue of type char, value {a, b}, rather a pointer to a, side effect of accessing the pointer
str2 // lValue array of type char, value being a pointer to letter c in memory
str1=str2 // non-lValue of type char, side effect is a reassignment of str1.
Q11
the first will loop 100 times, the second loops 99 times. (++ptr is clalculated to 100 and then checked by the while statement; it equates to arr+100, therefore reaching 100, and will not run for the 100th time.)
Q12
Personally, I still prefer array arithmetic,