Pointers and Arrays Flashcards
What are the 3 forms of pointer arithmetic
- Adding an integer to a pointer
- Subtract integer from point
- Subtract one pointer from another pointer
How does adding an integer to a pointer work
pointer is p and 3
if p points at a[2] and we say a q = p + 3, then q points to a[5]
note - you are not moving by 3 bytes, 3 is spaces in the array(c determines the bytes by type, so be careful of type casting)
How does subtracting one pointer with another work?
- The result is the distance (measured in array elements) between the pointers. The pointers need to point at array elements.
- If p points at a[5] and q at a[1] then the answer to p-q is 4 array elements
Undefined things happen if:
Pointers don’t point to array elements or elements from seperate arrays
How do you compare pointers
The outcome of comarison depends on relative positions
p = &a[5];
q = &a[1];
exmample;
p<= q is 0 or p>= q is 1
How do you use a pointer and an array with a compound literal?
int * p = (int []){3, 0, 3, 4, 1};
This saves us the hassle of having to type:
int a[] = {3, 0, 3, 4, 1};
int *p = &a[0];
How do you use a pointer to control/traverse a for loop?
define N 10
Basic:
int a[N], sum, *p;
sum = 0;
for(p =&a[0]; p<&a[N]; p++)
sum += *p;
Refined (using array name as pointer):
for (p = a; p < a + N; p++)
sum += *p;
When you use an array name as a pointer, what are the limitations?
it is not possible to assign the pointer a new value
example:
while(*a != 0)
a++;
How are arrays and pointers closely related with functions?
- When you pass an array to a function, you are passed a pointer(not the array itself), pointing to the start of the array (this is why we don’t know the length, it’s just a pointer, we could easily continue and access a part of the array that does not belong to us).
- Vs a primitive type, which is passed a COPY of the value.
- This means with primitives we aren’t chaning the original (in a function) but with arrays we ARE.
- This also means that passing the pointer, the size of the array doesn’t affect time to pass pointer into the function
- Because we are being passed a pointer, we could also be int *a (instead of the array name), the compiler treats it identical to int a[])
How do we write a function that is not supposed to change a value in an array it is pointing to?
int find_largest(const int a[]. int n)
{
}
If const is present, the compiler will check that no assignment to an element of a appears in the body of the function
What is the difference between:
int a[10];
and
int * a;
What would happen for each if for *a = 1;
- int a[10] allocated space for an array of 10 ints
- *a =1 is valid and would change the first element of the array
- int *a allocates space for a pointer only
- *a = 1 is dangerous because it could cause segmentation and no space has been allocated because it’s only a pointer, not pointing to an actual location of memory to change.
- If you see a segmentation fault it’s likely related to incorrect use of pointer
- to fix this call a=(int x)malloc(sizeof(int)
- then a will point at the valid space created by malloc
How do you pass a slice of an array to a function
largest = find_largest(&b[5], 10);
passes elements 5 through 14.
What is a void pointer and how is it used? What are the limitations
- a void pointer is a generic pointer
- essentially just a memory address and can be casted to actual type of data, this is how malloc works
- Cannot do pointer arithmatic or dereference(C needs the type for both of these operations)
define N 10
When using a pointer as an array name, what does the following code mean?
int a[N], i, sum = 0, *p = a;
for(i=0;i<n></n>
<p>sum+=p[i];</p>
</n>
The compiler treats p[i] as *(p+i)