1/13
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
type in array
all elements in an array need to be the same type
allocation of arrays
need to allocate space for all the elements in the array
eg. an array with 4 int elements would be initialized like int arr[4];
what happens when you call arr?
you receive a pointer to the first element of the array
how to instantiate an array?
int arr[3] = {0, 1, 2};it is cleaner to instantiate all the elements at the beginning and this is the only time you can do so, afterwards you must instantiate each element individually
size of array
an array cannot change size, if you need a larger array you would need to make a new one and copy all the elements into the new array from the original one
how to iterate through an array?
once we know the address of where the array starts, and the size of each element, we can calculate the address of each element
we rely on this fact when we use indexes to access elements of the array
big issue with arrays
C does not check that an array access is within the bounds of the array
what is a pointer?
stores the address of a variable
how to declare a pointer?
int *pt;this will create a pointer that points at an integer
how to store an address in a pointer?
int i;
int *pt;
pt = &i;using “&” allows you to access the address of a variable
how to access the value that is pointed to by a pointer?
int i = 209;
int *pt = &i;
printf("Value pointed to by pt: %d\n", *pt);using “*” in this context is called dereferencing a pointer
i and *pt are aliases
how do changes persist?
when we pass a pointer to the first element of an array rather than passing a copy of the array
arithmetic on a pointer
when we use the + operator on a pointer, its behaviour depends on the type of the pointer
pointer to pointer
to initialize and dereference we need to use **