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