Sie sind auf Seite 1von 159

Pointers

Deepanjan Kesh

int a;

int a;
Computer Memory

int a;
Computer Memory

int a;
Computer Memory

int a;
Computer Memory

256 GB

int a;
Computer Memory

256 GB = 34359738368 bytes

int a;
Computer Memory

256 GB = 34359738368 squares

int a;
Computer Memory

256 GB = 34359738368 unique squares

int a;
Computer Memory

Assign a unique address to each byte

int a;
Computer Memory

Assign a unique address to each byte


Numbers from 0 to 34359738367

int a;
Computer Memory

Assign a unique address to each byte


Numbers from 0 to 34359738367

int a;
Computer Memory

123456

Assign a unique address to each byte


Numbers from 0 to 34359738367

int a;
Computer Memory

int a;
Computer Memory

int a;
Computer Memory

int a;
Computer Memory

int a;
Computer Memory

a
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
5
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
101
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
00000000000000000000000000000101
123456

123457

123458

123459

int a;
Computer Memory
a = 5;

a
5
123456

123457

123458

123459

Data Types

Data Types

int

Data Types

int
char

Data Types

int
float
char

Data Types

pointer
int
float
char

Data Types

pointer
int
float
char

Pointer

Data Types

pointer
int
float
char

Pointer
Stores address of variables.

int a;
Computer Memory
a = 5;

a
5
123456

123457

123458

123459

int a;
p
a = 5;

Computer Memory

a
5
123456

123457

123458

123459

int a;
*p
a = 5;

Computer Memory

a
5
123456

123457

123458

123459

int a;
int *p
a = 5;

Computer Memory

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;

Computer Memory

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;

Computer Memory

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;
p = a;

Computer Memory

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;
p = a;

Computer Memory

a
5
123456

123457

123458

123459

int
int
a =
p =

a;
*p;
5;
&a;

Computer Memory

a
5
123456

123457

123458

123459

int
int
a =
p =

a;
*p;
5;
&a;

Computer Memory

a
5
123456

123457

123458

123459

int
int
a =
p =

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int
int
a =
p =

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int
int
a =
p =
p

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int
int
a =
p =
p

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int
int
a =
p =
*p

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int
int
a =
p =
*p

a;
*p;
5;
&a;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;
p = &a;
*p = 10;

Computer Memory

p
123456

a
5
123456

123457

123458

123459

int a;
int *p;
a = 5;
p = &a;
*p = 10;

Computer Memory

p
123456

a
10
123456

123457

123458

123459

Encore!
Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a)

Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10

Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf(" ", p)

Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p)

Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf(" ",
)

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf(" ", &a)

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p?

a
10
123456 123457 123458 123459

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p?

42

43

44

a
10
123456 123457 123458 123459

45

46

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p is 42 to 46.

42

43

44

a
10
123456 123457 123458 123459

45

46

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p is 42 to 46.
)
printf(" ",

42

43

44

a
10
123456 123457 123458 123459

45

46

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p is 42 to 46.
printf(" ", &p)

42

43

44

a
10
123456 123457 123458 123459

45

46

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p is 42 to 46.
printf("%p", &p)

42

43

44

a
10
123456 123457 123458 123459

45

46

Encore!
Every variable has some value.
printf("%d", a) 10
printf("%p", p) 123456

Computer Memory

p
123456

Every variable has an address.


Address of a is 123456 to
123459.
printf("%p", &a)
123456
Address of p is 42 to 46.
printf("%p", &p) 42

42

43

44

a
10
123456 123457 123458 123459

45

46

int a, b;

int a, b;

int a, b;
int *pa;

int a, b;
int *pa; /* pointer to a */

int a, b;
int *pa; /* address of a */

int a, b;
int *pa; /* address of a */

pa

int a, b;
int *pa; /* address of a */
int *pb;

pa

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */

pa

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */

pa

pb

int
int
int
int

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;

pa

pb

int
int
int
int

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;

b
t

pa

pb

int
int
int
int
a =

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;
5;

b
t

pa

pb

int
int
int
int
a =

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;
5;

5
t

pa

pb

int
int
int
int
a =
b =

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;
5;
10;

5
t

pa

pb

int
int
int
int
a =
b =

a, b;
*pa; /* address of a */
*pb; /* address of b */
t;
5;
10;

10
t

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;

10
t

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;

10
t

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

*pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

*pa

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t

pa

t = *pa;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;
pb

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;
pb

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;
*pb

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;
*pb

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10
t
5

pa

t = *pa;
*pa = *pb;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

10
t
5

pa

t = *pa;
*pa = *pb;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

10
t
5

pa

t = *pa;
*pa = *pb;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

10
t
5

pa

t = *pa;
*pa = *pb;
*pb = t;

pb

int a, b;
int *pa; /* address of a */
int *pb; /* address of b */
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

t = *pa;
*pa = *pb;
*pb = t;

pb

10

5
t
5

pa

t = *pa;
*pa = *pb;
*pb = t;

pb

10

5
t
5

pa

t = *pa;
*pa = *pb;
*pb = t;

pb

10

5
t
5

pa

swap(
{
t = *pa;
*pa = *pb;
*pb = t;
}

pb

10

5
t
5

pa

swap(int *pa, int *pb)


{
t = *pa;
*pa = *pb;
*pb = t;
}

pb

10

5
t
5

pa

void swap(int *pa, int *pb)


{
t = *pa;
*pa = *pb;
*pb = t;
}

pb

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;

10

5
t
5

pa

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

pb

int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;
swap(&a, &b);

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

10

5
t
5

pa

pb

int main(void)
{
int a, b;
int *pa;
int *pb;
int t;
a = 5;
b = 10;
pa = &a;
pb = &b;
swap(&a, &b);
return 0;
}
void swap(int *pa, int *pb)
{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

10

5
t
5

pa

pb

/* area and perimeter of a rectange */

/* area and perimeter of a rectange */


area peri(
{

/* area and perimeter of a rectange */


area peri(int len
{

/* area and perimeter of a rectange */


area peri(int len, int bth
{

/* area and perimeter of a rectange */


area peri(int len, int bth, int *ar
{

/* area and perimeter of a rectange */


area peri(int len, int bth, int *ar, int *pr)
{

/* area and perimeter of a rectange */


area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
}

/* area and perimeter of a rectange */


area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;
b = 3;

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;
b = 3;
area peri(l, b, &a, &p);

/* area and perimeter of a rectange */


void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;
b = 3;
area peri(l, b, &a, &p);
printf("area = %d, perimeter = %d\n", a, p);
}

#include <stdio.h>
/* area and perimeter of a rectange */
void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;
b = 3;
area peri(l, b, &a, &p);
printf("area = %d, perimeter = %d\n", a, p);
}

#include <stdio.h>
/* area and perimeter of a rectange */
void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}
int main(void)
{
int l, b;
int a, p;
l = 2;
b = 3;
area peri(l, b, &a, &p);
printf("area = %d, perimeter = %d\n", a, p);
return 0;
}

int a

int a

int *pa

int a
float b

int *pa

int a

int *pa

float b

float *pb

int a

int *pa

float b

float *pb

char c

int a

int *pa

float b

float *pb

char c

char *pc

int

int

int *

int a

int *pa

int a
float

int *pa

int a

int *pa

float

float *

int a

int *pa

float b

float *pb

int a

int *pa

float b

float *pb

char

int a

int *pa

float b

float *pb

char

char *

int a

int *pa

float b

float *pb

char c

char *pc

int a

int *pa

float b

float *pb

char c

char *pc

void swap(int *pa, int *pb)


{
int t;
t = *pa;
*pa = *pb;
*pb = t;
}

int a

int *pa

float b

float *pb

char c

char *pc

void swap(float *pa, float *pb)


{
float t;
t = *pa;
*pa = *pb;
*pb = t;
}

int a

int *pa

float b

float *pb

char c

char *pc

void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}

int a

int *pa

float b

float *pb

char c

char *pc

void area peri(int len, int bth, int *ar, int *pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}

int a

int *pa

float b

float *pb

char c

char *pc

void area peri(float len, float bth, float *ar, float


*pr)
{
*ar = len * bth;
*pr = 2 * (len + bth);
}