Pointers in C
What are addresses?
-They are the *memory locations* in which variables are stored Addresses can store: -Variables -Arrays -Files -Functions
What are pointers
-A pointer is a *variable* that contains a *memory address* instead of a actual variable value -They "point" to the memory location storing the variable value -Type of pointer is based on what kind of variable is *in the address* -Can also use pointers to store arrays files and functions
Example using & operator
-If we tell the compiler to printf &x, instead of printing the value in the address it will print the *actual number* of the address
Arithmetic of pointers
-We can perform arithmetic with pointer just like they're real variables -But if we perform operations with them we are changing the *addresses* they represent, unless the pointer is dereferenced -This becomes useful with *arrays* -The example in the photo also demonstrates how *derefrencing* a pointer works
How do we declare a pointer?
-We declare it just like any other variable, but we add a *star* before the pointer name -Once we declare it the pointers variable will be its address -Pointer type- will be the *type of variable* we are putting into it -Example- This example is putting the variable x in the memory location of y, x will still equal 18, but its memory location will be y
How do we specify the address of a pointer
-We set that pointer equal to the *address of a variable* by using the *& operator*
Dereferencing operator
-We use this to determine the *value* that is stored in the cell pointed to -If we add *another star* to a variable that is already defined as a pointer, it will return the value in its address
Referencing operator &
-What we use to *assign an address* to a pointer -Can be used to determine the *address* of the variable -If x is the variable, then &x will be the address of that variable -We use & in the scanf function in order to point the %f to the address for x
Setting a pointer equal to an array?
-Will set it as the address of the *first element* of the array, a[0]