Unit 2 CS 36 Flashcards

1
Q

Pointers

A

These values store the memory location of other variables (ex: int* yPtr = &y;)

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

Variables

A

These store values at a certain memory location (ex: int y = 5;)

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

What can pointers only be initialized to?

A

They can only be initialized to NULL or an existing variable

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

Are pointers read from left to right or right to left?

A

Pointers are read from right to left. For example, in the phrase int *yPtr = &y(address y), yPtr is pointing to data type int, or an object of data type int)

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

Dereferencing

A

When pointers are used to access the variable they point to, it is called dereferencing the pointer(ex: int y = 7;, int * yPtr = &y; int a = *yPtr;, now a is equal to 7)

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

Pass by pointer

A

Function using pointers as arguments. When an argument is passed into a parameter, it is in the form of an address of an integer or whatever (ex: &variablename). You would have to create that variable in the main method (ex: int variablename = 0;) or whatever

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

How to initialize an int array with the length of 5, and put five values in it

A

int array1[5] = {1, 2, 3, 4, 5}; (if you don’t want to put any values initially, you can do int array1[5]; and then do array1[0] = 1; etc…)

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

How to initialize the pointer value for an int array “v” (two ways)

A

You can do int *vPtr = v; or int *vPtr = &v[0]; (this means that v contains the memory address of the first element of v, as seen using the “&” sign)

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

How does storing a value inside of and doing any arithmetic stuff with a pointer variable work?

A

Let’s say we initialize an array that is int array1[5] = {1, 2, 3, 4, 5}; and int * array1Ptr = &array1[0]; or int * array1Ptr = array1;. If the address of the first element is equal to 3000, and we add 2 to the pointer, then the pointer should point to the third element of the array, and since it is an int array, each time you go up an element in it, it’s address would increase by 4 bytes, so now array1Ptr would equal to 3008.

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

Pointer Offset Notation

A

Let’s say you initialize an array like int array1[5] = {1, 2, 3, 4, 5};, and int * array1Ptr = array1; or int * array1Ptr = &array1[0];, then when you do int v = *(array1Ptr + 3);, the pointer will point to the fourth element of the array, and through dereferencing the address sign will cancel out, resulting in v equaling to the value of the fourth element. you can also do int v = *(array1 + 3); and you would get the same result

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

Pointer Index Notation (true or false, for an array, do you need to dereference the element address in order to access that value and set it equal to a variable?)

A

False, you can just do int x = array1Ptr[3]; or whatever, because int * array1Ptr = array1; (don’t need an address sign, is automatically equal to the first element of the array). This is because array variables hold addresses

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

What does const mean(not an argument)

A

The value of the variable or array or whatever remains constant and cannot change regardless of what code comes next

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

How to define SIZE

A

Below #include <stdio.h> before the main method, you would put #define SIZE 24; or whatever the array size is</stdio.h>

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

Array of Pointers

A

You can initialize an array of pointers, for example like char* array1[5] = {“the”, “for”, “way”, “hello”, “bye”}; in which all of the elements are pointers to the object of char. Each element here contains a pointer to a character string

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

How would you make a pointer a return value in a function?

A

You would have int* value1(int* values, int* values); in the function definition or something, later you would do return aPtr;, which is an address, and store that in a pointer variable in the main function

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

Dynamic Memory Allocation

A

Consists of the Stack (Function Call Stack) that is the main function and the local and parameter variables (local variables are variables that are defined inside of that function and are only active for that time the function is being used)

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

What is the difference between pointer array and regular array?

A

Regular arrays are static in size, while pointer arrays are dynamic unless you declare it as const. Also, char pointer arrays can store more than one character per element

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

When creating the array variable char planet1[] = {‘M’, ‘e’, ‘r’, ‘c’, ‘u’, ‘r’, ‘y’}; how many bytes does it contain?

A

It contains a total of 8 bytes, because each char value is equal to 1 byte, AND there is a terminating value at the end that is \0, that counts for one byte.

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

Where are bytes of memory reserved?

A

On the stack

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

String literal

A

When declaring a character array or printing a value, it can be either printf(“Hello”); or char helloArray[5] = “Hello”; (can even do it without the 5)

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

What is the different between a char pointer array and a normal char array

A

When you assign a string literal to the char pointer array (ex: char *array1[] = “hello”;), the value of array1 is constant and will never change(you can’t do array1[0] = ‘e’;). best practice is to do const char *array1[] = “” whatever value

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

True or false: when an array gets referenced in scanf, they do need in address

A

False, you can do char array1[]; and then scanf(“%s”, array1); (without the [])

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

&variable1

A

The memory location of the variable, variable1 (what the “&” sign is for)

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

Remember this: memory location means address

A

I rembor

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

How to pass a pointer as an argument for a function(pass by pointer)?

A

In the main method, you would declare int variables or whatever variables and put the address of the variable as the argument to equal the parameter variables

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

Non-const pointer to non-const data

A

When you have a pointer array or a normal char array, and you put it as an argument for a function, then you don’t even need the & sign, because it is already an address. This is also when there is no const in the parameter list or variables

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

non-const pointer to const data

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

True or false, pointers are left from left to right.

A

False, when you have a pointer initialized, like int * yPtr = &y;, you read it like yPtr points to the int object of y, so it is read from right to left

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

Character constant

A

A single character that is in single quotes that represents an integer value. For example, ‘z’ represents the integer value of z

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

null character

A

What the ending value of strings are “\0”

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

What is the size of char array1[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’}; and
char array2[] = “hello”;

A

The first is five bytes, because each char is one byte, and the second is 6 bytes because it is a string literal and ends with \0. If the first ended with \0, it would also be 6 bytes

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

True or false, when you create an array (ex: int array1[3];) and you have scanf(“3d%”, array1);, you do not need the address for array1

A

True, array1 is already an address

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

scanf contains the maximum characters or int or whatever you can input, and printf contains the minimum characters you can print out, and will print blank spaces for how many you missed off of the minimum

A

True, if you do scanf(“19d”, &value1); and you input a 20 digit value, it will store everything except that last integer, whereas if you do printf (without the address), it represents the minimum value, so for example min is 4 digits, when you do 24, it will print out two spaces and then print 24.

34
Q

In scanf(“%19s”, word);, and word[20];, why don’t you need an address before word and why is it 19 characters max instead of 20?

A

Because since you are inputting a string literal, the last character is \0, that counts as one byte to, so the max you can input is 19. Also, since word (without array) already points to the first element of the array, it already serves as an address or pointer.

35
Q

<ctype.h> (remember it has c at the front, that's when you know it's for characters)
</ctype.h>

A

This is another standard library (like <stdio.h>) that has a lot of functions for character values. You have to put an int value as an argument because characters are represented as int. Ex:
#include <stdio.h>
#include <ctype.h></ctype.h></stdio.h></stdio.h>

int main(void) {
char c = ‘u’;
printf(“%c”, toupper(c));
}

35
Q

<stdlib.h>
</stdlib.h>

A

Converts strings to floating-point numbers. (Review this this is kind of complicated it’s like pointer to a pointer)

36
Q

<stdio.h>
</stdio.h>

A

Standard io library

37
Q

fgets function

A

The parameters are fgets(char * array1[], int max, and FILE stream) and it reads the characters until the max (one less than put in because of /0) or until the end of the file.

38
Q

What is the difference between pointer arrays and normal arrays? And everything you need to know about pointer arrays (VERY IMPORTANT and PRACTICE THIS)

A

You can declare a pointer array either like

int v[5] = {1, 2, 3, 4, 5};
int* vPtr = v; (because JUST v without an array points to the first element of an array, so it points to 1)
or
int* vPtr = &v[0];
if you do int x = (vPtr + 3);, then x = 4 because vPtr + 3 points to the fourth element of the array, which is dereferenced, so it is equal to 4.
int x = (v + 3) also has the same purpose because int vPtr = v; and int
is a type
So a pointer to an array can be used like this, and essentially points to different elements and can be dereferenced in order to get the element from an array. Remember that just the array value can be used as a pointer
WHEREAS
Arrays of Pointers:

39
Q

What are the three types of streams?

A

Input (whatever you type(keyboard), or interact with the code), output is the result of the code(screen) or the inputs, and the error is also come across by the screen

40
Q

What is an example of an output stream?

A

printf(). It always contains a string as an argument, but we can have format controls to have variables in the printf function and other things

41
Q

What are the five types of format controls?

A

flags, field widths, conversion specifier, length modifier, precisions

42
Q

field width

A

it’s between the % and the conversion specifier (i, d or the type) that represents the minimum length of what the variable or thing should be. If it is lower than the minimum length, it will print that many whitespaces from the left for what is missing

43
Q

precision

A

This is usually a decimal point before the field width. For integers, it prints out the leading 0s for how many integers are needed further.

44
Q

How to define structs and how to create struct variables?

A

struct name {
// define pointers here
ex: char * hello1;
} (these are called the structures’s members)
int main(void){
struct name variable1;
}; (each structure must end with a semicolon)

45
Q

How to assign variables in a struct?

A

For the function above, you would have to do variable1.hello1 = “String Value”; in the main function too

46
Q

Structures

A

collections of related variables under one name. they may contain variables of many different data type values, which is the benefit in comparison to pointer arrays

47
Q

malloc() and free() (part of heap)

A

It basically asks the computer for free space for information to be stored into, and then a memory address of a variable can be stored into it. What goes in as an argument tells the computer how many bytes to store, and free usually has a pointer as an argument and frees up the storage needed to what the pointer points to.

ex:
int * newPtr = malloc(sizeof(int));
*newPtr = 9; //tells which value the pointer should point to, through the deferencing operator
free(newPtr); // frees up the storage needed for the pointer to point to the value. Now it points to nothing

Make sure to include the type and the * before the start of malloc (ex: (char)malloc(sizeof(char)5) //doing times 5 because there are a total of 5 characters in the array

48
Q

How this connects back to scope?

A

In a way, this is how long the memory is active for before the free function is called

49
Q

How to print out a double quote in C?

A

you would use the /” to print this out. ex(printf(“%s”, “/””);

50
Q

How to print out a percentage in C?

A

You would use “%%” to print out one percentage

51
Q

How to print out a \?

A

You would use \ (double slash) to print out one slash, just like percentage

52
Q

True or false: both a struct type variable and a struct type pointer can be declared as members within the same structure

A

False, a variable of type struct cannot be declared within that structure

53
Q

What is a structure variables, and how to define them?

A

A structure variable doesn’t reserve any space in the computer (like int types) and instead acts as it’s own data type. For example, if there is a structure declared structure structure1, you would do structure structure1 variable1[5] or *variable1[5] or whatever

54
Q

What are the two operators used to access members(variables inside of the brackets) of structures?

A

The structure member operator(.) and structure pointer operator (—>)

55
Q

What are structure variables used for?

A

They are used to access the members of a structure. For example, if there is a variable in a class called suit, you would do the structureVariable.suit to access that variable within the main function. If you have a structure variable that is a pointer, you would do structureVariable—>membervariable. you could also use dereferencing to do *structureVariable—>membervariable or suit. If a member of a structure is initialized but not assigned to anything, you can take a structure variable and use arrow or dot that member and set it equal to something in the main method

56
Q

typedef

A

This is an alias for the same name of the structure and can be used to create variables efficiently of the same struct class without using the key word struct. For example, lets say you declare a class
struct structure1{

}

57
Q

alias

A

When you make a typedef struct, and make a variable of that typedef struct, it is an alias for the actual struct.
For example, if you have
typedef struct{
int variable1;
int variable2;
} alias1;
You can use the alias (alias1) to do this
alias1 newstructvariable; and use that as a structure variable to access members of a class

58
Q

strcpy

A

Used to store a string into a char member from a structure in the main method. For example, strcpy(structurevariable1.memberVariable, “stringname”);

58
Q

What is another way to create an instance other than doing structurevariable1.memberVariable = whatever value

A

You can do nameofStructure structurevariable1 = {assignment1, assignment2, assignment3}; in the order of how the member variables are initialized in the structure (so for strings you can just assign it like that, and don’t have to do strcpy).

59
Q

How to create a structure variable that is a pointer?

A

You would have to do structure structurename *variableName (unless you created an alias, in which you don’t have to include structure)

60
Q

If there are a few initializers for a structure variable, what are the rest initialized to

A

0 or NULL

61
Q

How to declare a typedef without declaring a function?

A

instead of declaring a variable, for example struct nameofStruct variableName, you would instead do typeDef struct nameofStruct alias, and then you can use the alias to create structure variables(ex: alias variableName = {…}, or alias variableName; and then variableName.member = …;) (removes the struct)

62
Q

How to print out something in octal and hexadecimal

A

You can do %o and %x

63
Q

True or false: octal prints out the 0 at the start and hexadecimal prints out the 0x or 0X

A

False, you have to manually add that in the print statement

64
Q

How to print integers with precision?

A

You can do %.howmanydigitsintotal(i). for example, if there is a number of 873, and you wanted to print out 0873, there is a total of four digits, so you would do printf(“%.4i”, variableName);

65
Q

How to print out decimals with precision?

A

.4f (instead of i, float vs. integer) means four digits after the decimal point

66
Q

Scientific notation

A

.3e is three digits after the decimal point and before e is hit

67
Q

Remember decimal means in printing that you are adding digits after or before the thing, or scrutinizing the variable because remember, no decimal point is the minimum amount that is printed, and prints out spaces before if the minimum amount is not in the variable to = the min space

A

Yes

68
Q

fgets

A
69
Q

Linked lists

A

A linear collection of self-referential structures, called nodes, connected by pointer links

70
Q

Stacks

A

Insertion and deletion is only made at one end of the list, the top

71
Q

Queues

A

Insertion and deletion are made at the back and the front (head and tail)

72
Q

Binary trees

A

High speed searching and sorting

73
Q

Self-referential structure (link)

A

When a member of a function is a pointer that is declaring the name of the function, thus the name “self-referential”

74
Q

What do dynamic memory allocation need?

A

They need malloc and free. They need the ability for the pointer to either free up or need more space because these lists can change in size. They can do this through malloc(sizeof(int));

75
Q

What happens if you use malloc and there are not enough bytes to be used?

A

Then the pointer is equal to NULL, because there is not enough memory

76
Q

What is the last node of a list usually equal to?

A

It is equal to NULL

77
Q

How to create a linked list in C?

A

You do:
struct node{
char a;
struct node *abc;
}node;
The character is what you want to put into the list and the link is the node

78
Q

How to insert something into a linked list?

A
79
Q

What do you do after you have declared nodes

A

You create a function that creates a linked list

80
Q
A