Pointers - Part 1 Flashcards

1
Q

1 Pass By Value

A

Func(x, y)
func recieves copies of parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2 pass by reference

A

func(&x, &y)
func recieves address to parameter, can change original parameter value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 Local Variable:

A

only accessible within a specific part of a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4 Automatic Variable

A

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;}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

5 Static Variable

A

Preserves value even if out of scope. doesn’t need to reinitialize in case of new scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

6 String in C

A

character sequence terminated with null character ‘\0’. stored as array of characters
char str[]=”bob”;
char str[4] = {‘b’, ‘o’, ‘b’, ‘\0’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7 Pointer:

A

int *pointer = x
pointer == &x //address of x
*pointer == x //value of x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

8 Dynamic Memory Allocation

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9 Function Arguments

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

10 Dangling Pointer

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

11 NULL

A

C NULL: special reserved pointer value that does not refer to any valid data object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

12 Interface

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13 Stack Variable

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

14 Data Variable

A

containers for storing data values.

static, global variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

15 Heap “Variable”

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

16 Array

A

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

collection of variables of the same type

17
Q

17 Array Dynamic Allocation

A

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
Q

18 Forward Declaration

A

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.

19
Q

Function1 printf

A

It is used to show output on the screen

printf(“Hello World!”);

20
Q

Function2 scanf

A

It is used to take input from the user

scanf(“format_specifier”, &variables)

example-

int a;
scanf(“%d”,&a);
printf(“%d”,&a);

21
Q

Function3 sprintf

A

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.

22
Q

Function4 assert

A

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.”

23
Q

Function 5 malloc

A

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.

24
Q

Function 6 free

A

memory allocated with malloc() is not de-allocated automatically, so you need to run free() to remove deallocate memory.

free(ptr);

25
Q

Q1 What is the ILRD convention for function names?

A

TitleCase, AKA PascalCase

26
Q

Q2 What is the difference between using assert and if on parameters

A

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);

27
Q

Q3 What are the escape characters for printf? For scanf?

A

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)

28
Q

Q4 What is a dangling pointer?

A

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

29
Q

Q5 What will happen if you use a dangling pointer?

A

you cause a segmentation fault (use free)
also create a core dump file, yay

30
Q

Q6 a What will the following snippet print? b. What will happen if you do not cast ( (unsigned int*)fp; )?

A

a: type promotion is going to look… weird.
b: 3incompatible type, can’t cast to begin with.

31
Q

Q7 What will the following code print?

A

33 apparently

32
Q

Q8 What will the following code print?

A

3

33
Q

Q9 Explain the following:

A

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)

34
Q

Q10

A

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.

35
Q

Q11

A

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.)

36
Q

Q12

A

Personally, I still prefer array arithmetic,