pointers Flashcards

1
Q

how do you print a pointer

A

%p

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

how do you reference a variables pointer?

A

&num1;

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

how do you de-reference a pointer

A

*pointer;

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

how do you define a pointer

A

int * numPointer;

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

why use a pointer?

A

to pass by reference, when you want more permanence. Think about how this affects an array or linked list in java, it gets passed by reference and any changes we make, change the original object we referenced

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

Parameters in functions are passed as what?

A

The function makes a copy, this is called passing by value

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

What is the advantage of passing by reference

A

You can go to the actual variable and change it. (It’s not a local copy)

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

How do you code goodSwap( )?

A

void goodSwap(int *a, int *b){

int temp = *a;

*a = *b;

*b = temp;

}

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

How do you make a pointer constant?

A

int * const const_ptr;

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

How do you make the data a pointer points to to be constant?

A

int value;

const int *const_value = &value;

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

How do you make a pointer that is both a const pointer and const value?

A

int value;

int const * const Ptr= &value;

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

What is memory corruption?

A

If the pointer passed to free doesn’t point to a heap-allocated block, or if that block has already been freed, bad thing will happen, it will crash if you are lucky, rather than silently corrupting something

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

How do you use free( )?

A

free will be passed a pointer to an unneeded memory block:

p = mallo(…);

free(p);

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