Basics of Pointers and Arrays
Basics of Pointers and Arrays
Key Points (excuse the pun)
1) How memory allocation works
2) How to create pointers to variables in memory in c
3) How to differentiate between an address in memory for a variable and its value (referencing / dereferencing)
4) How to create arrays and why arrays are really just pointers
Finding memory addresses in c
Output of code:
&x -> memory address of variable x (this is called referencing a variable)
Finding memory addresses in c
Output of code:
&x -> memory address of variable x (this is called referencing a variable)
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
In c the character * denotes an integer that points to an address.
This is what we call “dereferencing” a “pointer”
Pointers
A pointer stores a memory address.
The type of the pointer is used by the compiler to see how much memory is stored there.
In c the character * denotes an integer that points to an address.
This is what we call “dereferencing” a “pointer”
Note that c DOES NOT enforce safety with pointers.
You can do many twisted and perverse things with pointers
Arrays
Int a[4] -> array of 4 integers. char c[4] -> array of 4 characters.
Arrays
Int a[4] -> array of 4 integers. char c[4] -> array of 4 characters.
An array is actually really a pointer to a block of memory.
Arrays
Int a[4] -> array of 4 integers. char c[4] -> array of 4 characters.
An array is actually really a pointer to a block of memory.
C does not prevent you from accessing a[5]
(because the address is a valid part of mem)
You may get a segmentation fault if the
memory is protected
Demo
Now onto the most dangerous part of the lecture: the live demo!
Stack frame
The stack frame keeps track of all the
memory that has been allocated and
tracks the address of each variable.
Stack frame
The stack frame keeps track of all the
memory that has been allocated and
tracks the address of each variable.
Stack frame
The stack frame keeps track of all the
memory that has been allocated and
tracks the address of each variable.
Stack frame
The stack frame keeps track of all the
memory that has been allocated and
tracks the address of each variable.
/docProps/thumbnail.jpeg