pointers Flashcards
how do you print a pointer
%p
how do you reference a variables pointer?
&num1;
how do you de-reference a pointer
*pointer;
how do you define a pointer
int * numPointer;
why use a pointer?
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
Parameters in functions are passed as what?
The function makes a copy, this is called passing by value
What is the advantage of passing by reference
You can go to the actual variable and change it. (It’s not a local copy)
How do you code goodSwap( )?
void goodSwap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
How do you make a pointer constant?
int * const const_ptr;
How do you make the data a pointer points to to be constant?
int value;
const int *const_value = &value;
How do you make a pointer that is both a const pointer and const value?
int value;
int const * const Ptr= &value;
What is memory corruption?
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 do you use free( )?
free will be passed a pointer to an unneeded memory block:
p = mallo(…);
free(p);