0% found this document useful (0 votes)
15 views30 pages

6 Pointer

Uploaded by

samiularefin2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

6 Pointer

Uploaded by

samiularefin2000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Pointers

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:

Variable Content Locatio


name s n

X 547 4000

ptr 4000
4036

According to above figure, By the help of ptr variable we


stored the address of variable X in address 4036
5
Pointers are used in following
variables
 Variables of any basic data type

 An Array

 Functions

 Structures, and

 Unions

6
Advantages of Pointers
 To point to different data structures

 To achieve clarity and simplicity

 More compact and efficient coding

 To return multiple values via functions

 Dynamic memory Allocation

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;

From the previous example:


int x=547,*ptr;
ptr=&x;
9
Use of & and *
When is & used?

When is * used?

& -- "address operator" which gives


or produces the memory address of a
data variable
* -- "dereferencing operator" which
provides the contents in the memory
location specified by a pointer
10
#include <stdio.h> printf("x = %d\n", x);
#include <stdlib.h> printf("&x = %d\n", &x);
printf("ptr = %d\n", ptr);
int main() printf("*ptr = %d\n", *ptr);
{ printf("&*ptr = %d\n", &*ptr);
int x; system("PAUSE");
int *ptr;
return 0;
x = 10; }

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

An integer may be added to or


subtracted from a pointer.

Pointervariables may be subtracted


from one another.

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.

For example, if the pointer valptr contains


the address of a double precision variable
and that address is 234567870, then the
statement:
valptr = valptr + 2;
would change valptr to 234567886
15
#include <stdio.h> ptr = ptr +1;
#include <stdlib.h> printf("After ptr increment\
int main() n");
{ printf("-------------------\n");
int x; printf("x = %d\n", x);
int *ptr; printf("&x = %d\n", &x);
printf("ptr = %d\n", ptr);
x = 10; printf("*ptr = %d\n", *ptr);
ptr = &x; printf("&*ptr = %d\n", &*ptr);
*ptr = *ptr + 1; system("PAUSE");
return 0;
printf("before ptr increment\ }
n");
printf("--------------------\n");
printf("x = %d\n", x);
printf("&x = %d\n", &x);
printf("ptr = %d\n", ptr);
printf("*ptr = %d\n", *ptr);
printf("&*ptr = %d\n\n\n",
&*ptr); 16
Pointers and One-Dimensional
Array
Array name is a pointer to the
first element.
◦ Address of first array element is
&x[0] or x.
◦ Address of 2nd element is &x[1] or
(x+1).
◦ So, address for i element is &x[i] or
(x+i).
Then to retrieve the content of i
element is x[i] or *(x+i).
17
#include <stdio.h>
for (i = 0; i <= 9; ++i)
#include <stdlib.h>
{
printf("\n i = %d\tx[i] = %d\t*(x+i) =
int main() %d", i, x[i], *(x+i));
{
static int x[10] = printf("\t &x[i] = %X\t x+i = %X\n", &x[i],
{10,11,12,13,14,15,16,17,18,19}; (x+i));
}
int i; system("PAUSE");

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]

Noted that for


int x[ ][20];
It become
int (*x)[20];

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>

char x[] = "Externally"; char *x = "Externally";

int main() int main()


{ {
static char y[ ] = "Internally"; static char *y = "Internally";

printf("\n%s", x); printf("\n%s", x);


printf("\n%s\n", y); printf("\n%s\n", y);

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

You might also like