Unit 2 CS 36 Flashcards
Pointers
These values store the memory location of other variables (ex: int* yPtr = &y;)
Variables
These store values at a certain memory location (ex: int y = 5;)
What can pointers only be initialized to?
They can only be initialized to NULL or an existing variable
Are pointers read from left to right or right to left?
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)
Dereferencing
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)
Pass by pointer
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 to initialize an int array with the length of 5, and put five values in it
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 to initialize the pointer value for an int array “v” (two ways)
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 does storing a value inside of and doing any arithmetic stuff with a pointer variable work?
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.
Pointer Offset Notation
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
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?)
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
What does const mean(not an argument)
The value of the variable or array or whatever remains constant and cannot change regardless of what code comes next
How to define SIZE
Below #include <stdio.h> before the main method, you would put #define SIZE 24; or whatever the array size is</stdio.h>
Array of Pointers
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 would you make a pointer a return value in a function?
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
Dynamic Memory Allocation
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)
What is the difference between pointer array and regular array?
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
When creating the array variable char planet1[] = {‘M’, ‘e’, ‘r’, ‘c’, ‘u’, ‘r’, ‘y’}; how many bytes does it contain?
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.
Where are bytes of memory reserved?
On the stack
String literal
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)
What is the different between a char pointer array and a normal char array
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
True or false: when an array gets referenced in scanf, they do need in address
False, you can do char array1[]; and then scanf(“%s”, array1); (without the [])
&variable1
The memory location of the variable, variable1 (what the “&” sign is for)
Remember this: memory location means address
I rembor
How to pass a pointer as an argument for a function(pass by pointer)?
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
Non-const pointer to non-const data
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
non-const pointer to const data
True or false, pointers are left from left to right.
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
Character constant
A single character that is in single quotes that represents an integer value. For example, ‘z’ represents the integer value of z
null character
What the ending value of strings are “\0”
What is the size of char array1[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’}; and
char array2[] = “hello”;
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
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
True, array1 is already an address
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
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.
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?
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.
<ctype.h> (remember it has c at the front, that's when you know it's for characters)
</ctype.h>
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));
}
<stdlib.h>
</stdlib.h>
Converts strings to floating-point numbers. (Review this this is kind of complicated it’s like pointer to a pointer)
<stdio.h>
</stdio.h>
Standard io library
fgets function
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.
What is the difference between pointer arrays and normal arrays? And everything you need to know about pointer arrays (VERY IMPORTANT and PRACTICE THIS)
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:
What are the three types of streams?
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
What is an example of an output stream?
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
What are the five types of format controls?
flags, field widths, conversion specifier, length modifier, precisions
field width
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
precision
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.
How to define structs and how to create struct variables?
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)
How to assign variables in a struct?
For the function above, you would have to do variable1.hello1 = “String Value”; in the main function too
Structures
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
malloc() and free() (part of heap)
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
How this connects back to scope?
In a way, this is how long the memory is active for before the free function is called
How to print out a double quote in C?
you would use the /” to print this out. ex(printf(“%s”, “/””);
How to print out a percentage in C?
You would use “%%” to print out one percentage
How to print out a \?
You would use \ (double slash) to print out one slash, just like percentage
True or false: both a struct type variable and a struct type pointer can be declared as members within the same structure
False, a variable of type struct cannot be declared within that structure
What is a structure variables, and how to define them?
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
What are the two operators used to access members(variables inside of the brackets) of structures?
The structure member operator(.) and structure pointer operator (—>)
What are structure variables used for?
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
typedef
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{
}
alias
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
strcpy
Used to store a string into a char member from a structure in the main method. For example, strcpy(structurevariable1.memberVariable, “stringname”);
What is another way to create an instance other than doing structurevariable1.memberVariable = whatever value
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).
How to create a structure variable that is a pointer?
You would have to do structure structurename *variableName (unless you created an alias, in which you don’t have to include structure)
If there are a few initializers for a structure variable, what are the rest initialized to
0 or NULL
How to declare a typedef without declaring a function?
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)
How to print out something in octal and hexadecimal
You can do %o and %x
True or false: octal prints out the 0 at the start and hexadecimal prints out the 0x or 0X
False, you have to manually add that in the print statement
How to print integers with precision?
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);
How to print out decimals with precision?
.4f (instead of i, float vs. integer) means four digits after the decimal point
Scientific notation
.3e is three digits after the decimal point and before e is hit
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
Yes
fgets
Linked lists
A linear collection of self-referential structures, called nodes, connected by pointer links
Stacks
Insertion and deletion is only made at one end of the list, the top
Queues
Insertion and deletion are made at the back and the front (head and tail)
Binary trees
High speed searching and sorting
Self-referential structure (link)
When a member of a function is a pointer that is declaring the name of the function, thus the name “self-referential”
What do dynamic memory allocation need?
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));
What happens if you use malloc and there are not enough bytes to be used?
Then the pointer is equal to NULL, because there is not enough memory
What is the last node of a list usually equal to?
It is equal to NULL
How to create a linked list in C?
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
How to insert something into a linked list?
What do you do after you have declared nodes
You create a function that creates a linked list