Pointers
NOTE: You will cover this in CSSE132 or CSSE332 so this will just be a light overview
SECOND NOTE: a lot of the code here is written in pseudo code so it wont run
Pretend you one get one massive array and you are not allowed to make any new arrays
array = [0,0,0,0……]
Now say I want to store a “person object” where we store age, height, and weight.
Lets just simply put the three numbers right next to each other:
array = [``age,height,weight``,``age,height,weight``,``age,height,weight``,``age,height,weight``……]
TODO: add picture
If I were to get the 4th person in the list we would:
age = array[9]
height = array[9+1]
weight = array[9+2]
effectively the 4th person’s object lives at index 9
Now for example if we have a function that multiplies a person’s height and weight (BMI)
void height_times_weight(person){
...
}
to pass this “person object” we could do one of two things
- copy the 3 values (age, height, weight) into the function
- just pass in the index, 9, the person is stored at
For Option 1 this might work sometimes but what if the person object had 100 things, or 1000, or even more!
That would make our program very slow, so passing in a single value 9 might be much easier.
array = [0,0,0,0……]
void height_times_weight(person){
array[person]
...
}
int main(){
height_times_weight(9)
}
This idea of passing index is called pointers.
Syntax: int* p
to create a pointer just put a *
after the type
EX:
int* int_pointer;
float* float_pointer;
char* char_pointer;
Person* person_pointer;
in height_times_weight()
we would write it like this:
void height_times_weight(Person* person){
...
}
To get the “index” of something we use the &
symbol
Person person = {1,2,3};
// this is like storing 1,2,3 somehwere in array
// array = [..., 1,2,3, ...]
Person* person_pointer = &person;
// its like the indexOf function
// &person will simply return the index in array where person is stored
// like in the first example the 4th person was stored in array[9]
// so &person would return 9
Then to access what is in array:
Person person = {1,2,3};
Person* person_pointer = &person;
int age = *person_pointer; // this is the same as array[9]
//Note: this does not give height but
// will give you the next person in array (5th person).
// This is because of pointer math and you will cover this in CSSE132
*(person_pointer + 1);
we put a *
in front a pointer type to dereference or basically plug in the index into array[]
HOWEVER**:**
this can be dangerous:
EX:
Person person = {1,2,3};
char* person_pointer = &person;
again &person
gets the index its at in array, say 9 again.
But because its a char*
it thinks
array = [... 1,2,3...]
^
//hmmm... this "1" here must be a char
the computer will think the 1 is a char which is BADDDD
essentially the type gives the pointer “context”
it makes sense of what is stored in array.
Like how units next to a number give it context types next to some binary give it context
You can interpret 5 meters as 5 degrees C which might not make sense just like how you can interpret the int 65 as a character which would translate to the character A
. (Using an ASCII translation table) (Another table)
This is an artifact of how the data is stored in binary inside of memory. Computers do not know what an “A” or a “5” is, they only know what 1’s and 0’s are. So there are ways of storing different types of variables. A five can be represented in binary as “0101”, but the character “A” is could be encoded as “01000001”
Practice:
- do example of max of 2
int* max(int* a, int* b)
int add(int* a, int* b)
- basic function that mutates a variable
- what does this function do:
- warmup.c (from os)
- arraylist.c (from os)
- getting length of a c array