Lecture 12 - Introduction to Pointers
Lecture 12 - Introduction to Pointers
Introduction to
Pointers
1. Background and introduction to pointers
Difference with ordinary variables
Examples
int *ptr_int; // stores the address of an integer
variable.
char *ptr_char; // stores the address of an char
variable.
float* ptr_float; // stores the address of an float
variable.
The address of a data item (variable) can be obtained
using the addressof operator ‘&’.
Also called the ampersand operator.
Simply place the addressof operator in front of the
data item’s name.
Assuming intVar is an integer variable, recall the
usage of scanf statement:
scanf(“%d”, &intVar);
Reads an integer, and places on the memory address
represented by intVar.
Also, try printing this value:
printf(“%d”, &intVar);
We can initialize a pointer by storing in it the address
of another variable.
Example: