Pointers and Arrays Flashcards

1
Q

What are the 3 forms of pointer arithmetic

A
  1. Adding an integer to a pointer
  2. Subtract integer from point
  3. Subtract one pointer from another pointer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does adding an integer to a pointer work

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does subtracting one pointer with another work?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you compare pointers

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you use a pointer and an array with a compound literal?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you use a pointer to control/traverse a for loop?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When you use an array name as a pointer, what are the limitations?

A

it is not possible to assign the pointer a new value

example:

while(*a != 0)

a++;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are arrays and pointers closely related with functions?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we write a function that is not supposed to change a value in an array it is pointing to?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between:

int a[10];

and

int * a;

What would happen for each if for *a = 1;

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you pass a slice of an array to a function

A

largest = find_largest(&b[5], 10);

passes elements 5 through 14.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a void pointer and how is it used? What are the limitations

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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>

A

The compiler treats p[i] as *(p+i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly