pointers programming applications. pointer a pointer is a variable whose value is a memory address...

35
Pointers Programming Applications

Upload: baldwin-atkinson

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

PointersProgramming Applications

Page 2: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer• A pointer is a variable whose value is a

memory address representing the location of the chunk of memory on either the run-time stack or on the heap.

• Pointers have names, values and types.

• Value of p versus value pointed to by p

24

p

Page 3: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer

The definition of pointer is

Int *p; float *p1; char *p2;

Which means that p is a pointer to integer

data in memory and p1 is a pointer to floating

point data in memory

Page 4: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Declaring pointer variables

it is not enough to say that a variable is a pointer. You also have to specify the type of variable to which the pointer points ! int * p1; // p1 points to an integer float * p2; // p2 points to a float

Exception: generic pointers (void *) indicate that the pointed data type is unknown may be used with explicit type cast to any type (type *) void * p;

type * variable_name ;

Page 5: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer using To use the pointer: int *p, k;

K=10;P= &k;

Means that p has the address of k in memory Then &k means the address of k and *p

means the value in address p

Page 6: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplemain(){int v1=11, v2=22, *p;p=&v1;printf(“%d”, p);p=&v2;printf(“%d”, p);printf(“%d”, *p);}

Page 7: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplemain(){

int v1=11, v2=22, *p;p=&v1;printf(“%d”, p);*p=33;printf(“%d \n %d”, p, *p);

}

Page 8: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplemain(){

int v1=11, v2, *p;p=&v1;printf(“%d”, p);v2=*p;printf(“%d %d”, p, v2);

}

Page 9: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer int u=3, v;

int *pu, *pv;

pu= &u;

v= *pu;

Pv=&v;

Printf(“%d %d %d %d”, u, &u, pu, *pu);

Printf(“\n %d %d %d %d”, v, &v, pv, *pv);

Page 10: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer

int u1, u2, v=3;

int *pv;

u1 = 2 * (v+5);

pv = &v;

u2 = 2 * (*pv +5);

printf(“ %d %d”, u1, u2);

Page 11: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Traceint x,y; int * px;

x = 5; px = &x;

printf("x = %d\n", x); printf("x = %d\n", *px);

y = *px; printf("y = %d\n", y);

*px = 10; printf("x = %d\n", x);

printf("x = %d\n", *px); px = &y;

*px = 15; printf("x = %d\n", x);

printf("y = %d\n", y);

printf("what = %d\n", *px);

Page 12: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Traceint x,z; float y; char ch, *chp;

int *ip1, *ip2; float *fp;

x = 100; y = 20.0; z = 50;

ip1 = &x; ip2 = &z; fp = &y; chp = &ch;

ip2 = ip1; ip1 = &z; *ip1 = *ip2;

*ip1 = 200; *ip1 = *ip2 + 300; *fp = 1.2;

printf("%f\n", *fp);

printf("%d\n", ip1);

printf("%d\n", *ip1);

Page 13: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer and array If we define the array

int x[5]={2, 1, 5, 6, 8} The variable x only means pointer to the first

element of array x[] If we define pointer int *p; Then we can use p as

p = &x[0]

Which means that p is pointer to first element or p = x

Page 14: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer and array

Both x and p can be used as

printf(“%d”, *(x+2)); means the x[2]

printf(“%d”, *(p+1)); means the x[1]

printf(“%d”, (*x+2)); means the x[0]+2

Page 15: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer

Int x[8] = {10, 20, 30, 40, 50, 60, 70, 80,};

Printf(“%d %d %d %d %d”, x, x+2, *x, (*x+2),

*(x+2));

Page 16: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Example

float list[10], *p1;

for(int i=0; i<10;i++, p1++)

*p1=i;

for( i=0; i<10;i++)

printf(“%f”, *(pt-i));

Page 17: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Function and Pointer

We can call function by value f(s)

Or call function by reference f(&s)

Or define pointer p = &s and call function f(p)

Page 18: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Function and pointer

In the function call by value, any modification

held in the function will not affect the original

value

In call of reference, the modification in

function affect the original value.

Page 19: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplevoid set (int x);int main(void){int x=10,y=11;set(x); set(y);printf(“%d%d”, x,y);}void set (int x){int y=x+2; x*=2;}

Page 20: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplevoid set (int *x);int main(void){int x=10,y=11;set(&x); set(&y);printf(“%d%d”, x,y);}void set (int *x){int y=*x+2; *x*=2;}

Page 21: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Examplevoid set (int &);int main(void){int x=10,y=11;set(&x); set(&y);printf(“%d%d”, x,y);}void set (int &x){int y=x+2; x *=2;}

Page 22: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Passing arrayvoid cent (double *);void main(){double varray[5]={10.0, 43.1, 95.9, 59.7, 87.3};cent(varray);for(int j=0; j<5; j++) printf(“%d”, varray[j]);}void cent (double *pt){

for(int j=0; j<5; j++)*pt++ *= 2.54;

}

Page 23: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer & Stringvoid disp ( char *);void main() {char str[] = “Idel people have the …”;disp(str);}void disp (char *ps){while (*ps != “\0”)printf(“%c”, *ps++);}

Page 24: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Array of string pointer

We can define array of pointer to strings as:

char * arrstr[7]={“Saturday”, “Sunday”, “Monday”,

“Tuesday”, “Wednesday”, “Thursday”, “Friday”}

for (int j=0; j<7; j++) printf(“%s”, arrstr[j]);

Page 25: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Example

float list[10] ={1, 5, 6, 9, 10, 0, 45, 4, 61, 7};

float *p, *d;

P=list; d=&list[9];

for( i=0; i<10;i++)

printf(“%f”, *(p+i),*(d-i) );

Page 26: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

#include<stdio.h>Void push(int *, int);Void pop (int *);

Main(){ int *p, value;For(int t=0; t<5;t++) {scanf(“%d”, &value);

push(p, value);}Pop(p); pop(p);}Void push(int *p, int v){ p++; *p=v;}

void pop (int *p){printf(“%d”, *(p--));}

Page 27: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Trace#include<stdio.h>

Int f (int);

Void main()

{Int y=f(0);

Int z = f(4);

Printf (“ %d \t %d \n “, y, z);

}

int f(int x)

{if (x==0) return 0;

Else if (x>=1) return x+f(x-1);

}

Page 28: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Example

Using pointer, write a program that read a list

of numbers and sort them using swap function

which accept two pointers to elements in this

list and swap them.

Page 29: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Example

Write a program to read group of numbers

from the user and then average them after

stored them in an array and print the result.

You should use pointer as possible.

Page 30: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Example

Write a function String which accept as

argument the pointer to string and convert the

string to all upper case. You can use the

toupper() library function which takes a

single character and return this character in

upper case.

Page 31: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

NULL pointers Values of a pointer variable:

Usually the value of a pointer variable is a pointer to some other variable Another value a pointer may have: it may be set to a null pointer

A null pointer is a special pointer value that is known not to point anywhere. No other valid pointer, to any other variable, will ever compare equal to a null

pointer ! Predefined constant NULL, defined in <stdio.h> Good practice: test for a null pointer before inspecting the value pointed !

#include <stdio.h>

int *ip = NULL ;

if(ip != NULL) printf("%d\n", *ip) ;

if(ip ) printf("%d\n", *ip) ;

Page 32: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Never use uninitialized pointers.

To increment a dereferenced pointer, use

(*p)++

rather than

*p++ /* correct but means … */

Page 33: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Generic Pointers and NULL• Java: a reference to "any" kind of object use a variable of type Object• C: a reference to "any" kind of object use a variable of type void*

void *p;

defines a generic, or typeless pointer p. Often casted to (T*)p

• NULL value can be assigned to any pointer, no matter what its type.

Page 34: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Generic Pointers and NULL void *p = NULL;

int i = 2; int *ip = &i;

p = ip; printf("%d", *p);

printf("%d", *((int*)p ) );

Page 35: Pointers Programming Applications. Pointer A pointer is a variable whose value is a memory address representing the location of the chunk of memory on

Pointer Expressions and Pointer Arithmetic

pv+n pv + n*sizeof(variable type that pointer point to)

Arithmetic operations can be performed on pointers Increment/decrement pointer (++ or --)

Example:

++vPtr, vPtr++,

--vPtr, vPtr--

Add an integer to a pointer( + or += , - or -=) Pointers may be subtracted from each other Operations meaningless unless performed on an array