19-07-2013, 01:59 PM
C Tutorial - Pointers
C Tutorial.ppt (Size: 145.5 KB / Downloads: 713)
The Stack
The stack is the place where all local variables are stored
a local variable is declared in some scope
Example
int x; // creates the variable x on the stack
As soon as the scope ends, all local variables declared in that scope end
the variable name and its space are gone
this happens implicitly – the user has no control over it
Pointers
A pointer is simply a local variable that refers to a memory location on the heap
Accessing the pointer, actually references the memory on the heap
Declaring Pointers
Declaring a pointer is easy
declared like regular variable except that an asterisk (*) is placed in front of the variable
example
int *x;
using this pointer now would be very dangerous
x points to some random piece of data
declaring a variable does not allocate space on the heap for it
it simply creates a local variable (on the stack) that will is a pointer
use malloc() to actually request memory on the heap
Using a Pointer
To access a piece of data through a pointer, place an asterisk (*) before the pointer
example
char *ptr = malloc(1);
*ptr = ‘a’;
if(*ptr == ‘a’) { … }
Using the pointer without the asterisk actually accesses the pointer value
not the data the pointer is referencing
this is a very common mistake to make when trying to access data
Simple Example
Declare local variables x and y.
Declare local pointer z.
Allocate space on the heap for single integer. This step also makes z point to that location (notice the address of the space on the heap is stored in z’s location on the stack.
Set the local variable y equal to 5.
Follow the pointer referenced by z to the heap and set that location equal to 3.
Grab the value stored in the local variable y and follow the pointer z to grab the value stored in the heap. Add these two together and store the result in the local variable x.
Releases the memory on the heap (so another process can use it) and sets the value in the z pointer variable equal to NULL. (this step is not shown on the diagram)
One More Example
In one line, declare the pointer variable (gets placed on the stack), allocate memory on the heap, and set the value of the pointer variable equal to the starting address on the heap.
Read a value from the user into the space on the heap. This is why scanf takes pointers as the parameters passed in.
Release all the space on the stack pointed to by str and set the value of the str pointer on the stack equal to null. (step not shown)
scanf() Example
In one line, declare the pointer variable (gets placed on the stack), allocate memory on the heap, and set the value of the pointer variable equal to the starting address on the heap.
Create the local variable grade on the heap.
Read a value from the user into the space on the heap – beginning at the address indicated by the pointer variable on the stack.
Read a value from the user into the address referred to by the address of grade.
Release all the space on the stack pointed to by student and set the value of the student pointer on the stack equal to null. (step not shown)