Pointers
9/11/2017 1MRI
 Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
Variable a’s value, i.e., 100, is
stored at memory location 1024
100… … 1024 …
Memory address: 1024 1032
int a = 100;
…
1020
a
9/11/2017 2MRI
 A pointer is a variable used to store the
address of a memory cell.
 We can use the pointer to reference this
memory cell
100… … 1024 …
Memory address: 1024 1032
…
1020
integer
pointer
9/11/2017 3MRI
4
 Pointer stores the address of another entity
 It refers to a memory location
int i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */
9/11/2017 MRI
5
 ptr is a variable storing an address
 ptr is NOT storing the actual value of i
int i = 5;
int *ptr;
ptr = &i;
printf(“i = %dn”, i);
printf(“*ptr = %dn”, *ptr);
printf(“ptr = %pn”, ptr);
5i
address of iptr
Output:
i = 5
*ptr = 5
ptr =
effff5e0
value of
ptr =
address of i
in memory
9/11/2017 MRI
Pointer has the following advantages :-
Using pointers we can allocate memory dynamically to
structures (Dynamic memory allocation).
Arrays or strings can be passed to function more
efficiently.
Better memory management or our program will run
faster.
Pointer helps us to build complex data structures like
linked lists, trees etc.
Provides alternate way to access array elements of any
dimension.
Used to return more than one value from function.
Pointers can increase the execution speed of program.9/11/2017 6MRI
 Declaration of Pointer variables
data_type *pointer_name;
where
1. Data_type is the type of data pointed to (e.g. int,
char, double)
2. The asterisk (*) tells the variable is a pointer variable
3. Pointer_name needs a memory location
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer

9/11/2017 7MRI
9/11/2017 8MRI
Once a pointer variable has been declared we
can use the assignment operator to intialize the
varibale.
Example:
int quantity;
int *p;
p=&quantity;
 The "address of " operator (&) gives the memory
address of the variable
 Usage: &variable_name
 The address (&) operator can be used in front of
any variable object in C -- the result of the
operation is the location in memory of the
variable
100… … … …
Memory address: 1024
…
1020
a
9/11/2017 9MRI
A pointer can also be made to point to a pointer
variable (but the pointer must be of a type that
allows it to point to a pointer)
Example:
intV = 101;
int *P = &V; /* P points to intV */
int **Q = &P; /* Q points to int pointer P */
9/11/2017 10MRI
Pointers are generally of the same size (enough bytes
to represent all possible memory addresses), but it
is inappropriate to assign an address of one type of
variable to a different type of pointer
Example:
intV = 101;
float *P = &V; /* Generally results in aWarning */
Warning rather than error because C will allow you to
do this (it is appropriate in certain situations)
9/11/2017 11MRI
When assigning a memory address of a variable of one
type to a pointer that points to another type it is
best to use the cast operator to indicate the cast is
intentional (this will remove the warning)
Example:
intV = 101;
float *P = (float *) &V; /* Casts int address to float *
*/
Removes warning, but is still a somewhat unsafe thing
to do
9/11/2017 12MRI
A void * is considered to be a general pointer.
No cast is needed to assign an address to a void * or
from a void * to another pointer type.
Example:
intV = 101;
void *G = &V; /* No warning */
float *P = G; /* No warning, still not safe */
Certain library functions return void * results (more
later)
9/11/2017 13MRI
int A[5] - A is the address where the array starts (first
element), it is equivalent to &(A[0])
A is in some sense a pointer to an integer variable.
To determine the address of A[x] use formula:
(address ofA + x * bytes to represent int)
(address of array + element num * bytes for element size)
The + operator when applied to a pointer value uses
the formula above:
A + x is equivalent to &(A[x])
*(A + x) is equivalent to A[x]
9/11/2017 14MRI
float A[6] =
{1.0,2.0,1.0,0.5,3.0,2.0};
float *theMin = &(A[0]);
float *walker = &(A[1]);
while (walker < &(A[6])) {
if (*walker < *theMin)
theMin = walker;
walker = walker + 1;
}
printf("%.1fn",*theMin);
9/11/2017 15MRI
Array of Pointers & Pointers to Array
a
b
c
An array of Pointers
p
int a = 1, b = 2, c = 3;
int *p[5];
p[0] = &a;
p[1] = &b;
p[2] = &c;
int list[5] = {9, 8, 7, 6, 5};
int *p;
P = list;//points to 1st entry
P = &list[0];//points to 1st entry
P = &list[1];//points to 2nd entry
P = list + 1; //points to 2nd entry
A pointer to an array
9/11/2017 16MRI
 The name of an array points only to the first
element not the whole array.
1000
1012
1016
1004
1008
9/11/2017 17MRI
When passing whole array as parameter use syntax
ParamName[], but can also use *ParamName
Still treat the parameter as representing array:
int totalArray(int *A, int N) {
int total = 0;
for (I = 0; I < N; I++)
total += A[I];
return total;
}
For multi-dimensional arrays we still have to use the
ArrayName[][Dim2][Dim3]etc. form
9/11/2017 18MRI
 We can access to the value stored in the variable pointed
to by using the dereferencing operator (*),
10088 … 1024 …
Memory address: 1024 1032
…
1020
int a = 100;
int *p = &a;
 Result is:
100
1024
1024 100
1032
a p
9/11/2017 19MRI
 Declaring a pointer means only that it is a pointer: int
*p;
 Don’t be confused with the dereferencing operator, which
is also written with an asterisk (*).They are simply two
different tasks represented with the same sign
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 points to b
p2 = p1; // p2 points to a
b = *p3; //assign c to b
*p2 = *p3; //assign c to a
 Result is:
888
9/11/2017 20MRI

Pointers

  • 1.
  • 2.
     Each variableis assigned a memory slot (the size depends on the data type) and the variable’s data is stored there Variable a’s value, i.e., 100, is stored at memory location 1024 100… … 1024 … Memory address: 1024 1032 int a = 100; … 1020 a 9/11/2017 2MRI
  • 3.
     A pointeris a variable used to store the address of a memory cell.  We can use the pointer to reference this memory cell 100… … 1024 … Memory address: 1024 1032 … 1020 integer pointer 9/11/2017 3MRI
  • 4.
    4  Pointer storesthe address of another entity  It refers to a memory location int i = 5; int *ptr; /* declare a pointer variable */ ptr = &i; /* store address-of i to ptr */ printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */ 9/11/2017 MRI
  • 5.
    5  ptr isa variable storing an address  ptr is NOT storing the actual value of i int i = 5; int *ptr; ptr = &i; printf(“i = %dn”, i); printf(“*ptr = %dn”, *ptr); printf(“ptr = %pn”, ptr); 5i address of iptr Output: i = 5 *ptr = 5 ptr = effff5e0 value of ptr = address of i in memory 9/11/2017 MRI
  • 6.
    Pointer has thefollowing advantages :- Using pointers we can allocate memory dynamically to structures (Dynamic memory allocation). Arrays or strings can be passed to function more efficiently. Better memory management or our program will run faster. Pointer helps us to build complex data structures like linked lists, trees etc. Provides alternate way to access array elements of any dimension. Used to return more than one value from function. Pointers can increase the execution speed of program.9/11/2017 6MRI
  • 7.
     Declaration ofPointer variables data_type *pointer_name; where 1. Data_type is the type of data pointed to (e.g. int, char, double) 2. The asterisk (*) tells the variable is a pointer variable 3. Pointer_name needs a memory location Examples: int *n; RationalNumber *r; int **p; // pointer to pointer  9/11/2017 7MRI
  • 8.
    9/11/2017 8MRI Once apointer variable has been declared we can use the assignment operator to intialize the varibale. Example: int quantity; int *p; p=&quantity;
  • 9.
     The "addressof " operator (&) gives the memory address of the variable  Usage: &variable_name  The address (&) operator can be used in front of any variable object in C -- the result of the operation is the location in memory of the variable 100… … … … Memory address: 1024 … 1020 a 9/11/2017 9MRI
  • 10.
    A pointer canalso be made to point to a pointer variable (but the pointer must be of a type that allows it to point to a pointer) Example: intV = 101; int *P = &V; /* P points to intV */ int **Q = &P; /* Q points to int pointer P */ 9/11/2017 10MRI
  • 11.
    Pointers are generallyof the same size (enough bytes to represent all possible memory addresses), but it is inappropriate to assign an address of one type of variable to a different type of pointer Example: intV = 101; float *P = &V; /* Generally results in aWarning */ Warning rather than error because C will allow you to do this (it is appropriate in certain situations) 9/11/2017 11MRI
  • 12.
    When assigning amemory address of a variable of one type to a pointer that points to another type it is best to use the cast operator to indicate the cast is intentional (this will remove the warning) Example: intV = 101; float *P = (float *) &V; /* Casts int address to float * */ Removes warning, but is still a somewhat unsafe thing to do 9/11/2017 12MRI
  • 13.
    A void *is considered to be a general pointer. No cast is needed to assign an address to a void * or from a void * to another pointer type. Example: intV = 101; void *G = &V; /* No warning */ float *P = G; /* No warning, still not safe */ Certain library functions return void * results (more later) 9/11/2017 13MRI
  • 14.
    int A[5] -A is the address where the array starts (first element), it is equivalent to &(A[0]) A is in some sense a pointer to an integer variable. To determine the address of A[x] use formula: (address ofA + x * bytes to represent int) (address of array + element num * bytes for element size) The + operator when applied to a pointer value uses the formula above: A + x is equivalent to &(A[x]) *(A + x) is equivalent to A[x] 9/11/2017 14MRI
  • 15.
    float A[6] = {1.0,2.0,1.0,0.5,3.0,2.0}; float*theMin = &(A[0]); float *walker = &(A[1]); while (walker < &(A[6])) { if (*walker < *theMin) theMin = walker; walker = walker + 1; } printf("%.1fn",*theMin); 9/11/2017 15MRI
  • 16.
    Array of Pointers& Pointers to Array a b c An array of Pointers p int a = 1, b = 2, c = 3; int *p[5]; p[0] = &a; p[1] = &b; p[2] = &c; int list[5] = {9, 8, 7, 6, 5}; int *p; P = list;//points to 1st entry P = &list[0];//points to 1st entry P = &list[1];//points to 2nd entry P = list + 1; //points to 2nd entry A pointer to an array 9/11/2017 16MRI
  • 17.
     The nameof an array points only to the first element not the whole array. 1000 1012 1016 1004 1008 9/11/2017 17MRI
  • 18.
    When passing wholearray as parameter use syntax ParamName[], but can also use *ParamName Still treat the parameter as representing array: int totalArray(int *A, int N) { int total = 0; for (I = 0; I < N; I++) total += A[I]; return total; } For multi-dimensional arrays we still have to use the ArrayName[][Dim2][Dim3]etc. form 9/11/2017 18MRI
  • 19.
     We canaccess to the value stored in the variable pointed to by using the dereferencing operator (*), 10088 … 1024 … Memory address: 1024 1032 … 1020 int a = 100; int *p = &a;  Result is: 100 1024 1024 100 1032 a p 9/11/2017 19MRI
  • 20.
     Declaring apointer means only that it is a pointer: int *p;  Don’t be confused with the dereferencing operator, which is also written with an asterisk (*).They are simply two different tasks represented with the same sign int a = 100, b = 88, c = 8; int *p1 = &a, *p2, *p3 = &c; p2 = &b; // p2 points to b p2 = p1; // p2 points to a b = *p3; //assign c to b *p2 = *p3; //assign c to a  Result is: 888 9/11/2017 20MRI