6 Pointer
6 Pointer
1
Overview
What is pointer?
Pointer Declaration
Passing Pointer to Function
Operations in Pointers
2
What is pointer?
Pointers
are variables that contain
memory addresses as their values.
A variable name directly references a
value.
A pointer indirectly references a
value. Referencing a value through a
pointer is called indirection.
A pointer variable must be declared
before it can be used.
3
Concept of Address and Pointers
ADDR1 Contents1
• Memory can be ADDR2
conceptualized as a ADDR3
ADDR4
linear set of data ADDR5
locations. ADDR6
*
• Variables reference *
*
the contents of a
locations ADDR11 Contents11
• Pointers have a *
*
value of the address
of a given location ADDR16 Contents16
4
EXAMPLE FOR POINTER:
int X = 547;
HERE:
HERE:
X 547 4000
ptr 4000
4036
An Array
Functions
Structures, and
Unions
6
Advantages of Pointers
To point to different data structures
7
Declaring a Pointer
Variable
In previous example, ptr points to
the variable x. We say that ptr is
an integer pointer.
Similarly, we have character
pointer, floating pointer, long
pointer, … .
8
Declaring a Pointer
Variable
To declare ptr as an integer
pointer:
int *ptr;
To declare ptr as a character
pointer:
char *ptr;
When is * used?
ptr = &x;
*ptr = *ptr + 1;
11
Pointers in Functions
Previously in function, we learn to
pass value to function by
Passing/call by Value Passing/call by reference
1. int cubebyvalue(int); 1. int cubebyreference(int*);
2. main(){ 2. main(){
3. int a,d; 3. int a,d;
4. printf("\na = "); 4. printf("\na = ");
5. scanf("%d", &a); 5. scanf("%d", &a);
6. 6.
7. d = cubebyvalue(a); 7. d=cubebyreference(&a);
8. printf("\n\n%d,%d", 8.
a,d); 9. printf("\n\n %d,%d", a,d);
9. 10.
10. return 0; 11. return 0;
11. } 12. }
12. int cubebyvalue(int x) 13. void cubebyreference(int *xPtr){
13. { 14. return(*xPtr =*xPtr * *xPtr *
14. return(x * x * x); *xPtr);
15. } 15. }
12
Pointers are often passed to a
function as an argument
This is to allow data item can be
accessed by the function and
returned to the program
13
Arithmetic and Logical
Operations on Pointers
A pointer may be incremented or
decremented
Pointer
variables can be used in
comparisons, but usually only in a
comparison to NULL.
14
When an integer is added to or subtracted
from a pointer, the new pointer value is
changed by the integer times the number
of bytes in the data variable the pointer is
pointing to.
return 0;
}
18
Pointer and Multidimensional
Arrays
Since one dimensional array can
be represented by pointer (the
array name)
Pointer can also used in
representing multidimensional
array.
19
A multidimensional array can be
declare as below:
Datatype (*ptr)[expression1] [expression2]…
[expression i]
20
To retrieve content of one
dimensional array for array
declared using pointer, it will be
*(arrayname + i)
Example:
◦ to retrieve x[2][5] in array declared
using pointer, the code will be
*(*(x+2)+5)
21
Array and String
Since array name is the pointer
to the first element,
When dealing with strings, the
retrieval of the strings can be
done by calling the content
pointer to the array
* arrayname
22
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
system("PAUSE"); system("PAUSE");
return 0; return 0;
} }
23
Dynamic Memory
Allocation
Previously, the number of elements
already been defined when declaring
the array
But since a array name is actually a
pointer to the first element, it is
possible to define array as a pointer
variable rather than as a conventional
array.
In other meaning, you may no need to
fixed the number of elements within
an array.
24
This is called Dynamic Memory
Allocation
Generally it is done using a C
function called malloc (under
arrayname = (datatype *) malloc (quantity *
stdlib.h)
sizeof(datatype));
25
Reordering the content of
Array
The content of the array is entered by
user.
Sometime an ordered list / content are
required for further operation or
output.
The reordering can be done by
comparing two elements in the array
and swap their position if needed.
The process need to carry out to
compare and swap all the element to
be in ordered.
26
#include <stdio.h> /*Display the ordered list of numbers*/
#include <stdlib.h> /*rearrange the list of numbers*/
void reorder(int n, int *x); void reorder(int n, int *x)
int main() {
{ int i, item, temp;
int i, n, *x; /*find the smallest of al remaining
printf("\nHow many numbers will be elements*/
entered?"); for(item = 0; item < n - 1; ++item)
scanf("%d", &n); {
printf("\n"); for (i=item+1; i < n; ++i)
x = (int*)malloc(n * sizeof(int)); {
for(i = 0; i < n; ++i) if (*(x+i) < *(x+item))
{ printf("i = %d\t x = ", i+1); {
scanf("%d", x+i); /*interchange two elements*/
} temp = *(x+item);
reorder(n, x); *(x+item) = *(x+i);
printf("\n\nReordered list of Numbers:\ *(x+i) =
n\n"); temp;
for (i = 0; i < n; ++i) }
{ }
printf("i = %d\t x = %d\n", }
i+1, *(x+i)); }
}
system("PAUSE"); 27
28
Advance Reordering
Algorithm
Beside the reordering technique
shown, there are others more
complicated technique to
reordering the elements in a list /
array.
These techniques are called
sorting algorithm
Will be learn in Data Structure
and Algorithm (Senior called it
C++ class).
29
END
30