What are the 3 forms of pointer arithmetic
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?
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?
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;
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
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)