29
Introduction to Pointers These Slides NOT From Text

Introduction to Pointers These Slides NOT From Text

  • View
    231

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Introduction to Pointers These Slides NOT From Text

Introduction to Pointers

These Slides NOT From Text

Page 2: Introduction to Pointers These Slides NOT From Text

First … size of types

Everything in a computer program must fit into high speed memory.

High speed memory is simply a string of bytes. Lots and Lots and Lots of bytes, but just that…bytes.

Some number of bytes are allocated every time you define a variable.

Page 3: Introduction to Pointers These Slides NOT From Text

High Speed Memory (RAM)

1073741823

01

2345

1Gb of RAM

Page 4: Introduction to Pointers These Slides NOT From Text

So What Is The Size Of Different Types?

printf(“sizeof(int) = %d\n”, sizeof(int));

I ran a short program with a bunch of thesestatements.

• sizeof(int) = 4 sizeof(long) = 4

• sizeof(long int) = 4 sizeof(long long int) = 8

• sizeof(char) = 1 sizeof(float) = 4

• sizeof(double) = 8 sizeof(long double) = 12

Page 5: Introduction to Pointers These Slides NOT From Text

So What Is The Size Of Different Types?

• sizeof(int) = 4 sizeof(long) = 4

• sizeof(long int) = 4 (long long illegal on WinXp C++.Net)

• sizeof(char) = 1 sizeof(float) = 4

• sizeof(double) = 8 sizeof(long double) = 8

• Now, on to pointers!

Depends on brand of OS and compiler.

Here is Visual Studio .Net on my Windows Xp Professional. (Differences are marked in red.)

Page 6: Introduction to Pointers These Slides NOT From Text

What is a pointer?

A variable containing the address of another variable. (Hence it “points” to the other location).

There are “things” and “pointers to things”

Page 7: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

int x=2,y=4,z=6;memory

Address shown in hex.

name address

Page 8: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C Z

int x=2,y=4,z=6;memory

name address

Page 9: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x=2,y=4,z=6;2

memory

4

6

name address

Note, I have shown the value of the variable in memory, not the actual representation!

Page 10: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x=2,y=4,z=6;

int * px = &x;

memory

2

6

4

name address

Page 11: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px

memory

px

0F45AB24

2

6

4

0F45AB14

name address

Page 12: Introduction to Pointers These Slides NOT From Text

So, what is the size of a pointer? sizeof(int *) = 4 sizeof(long long int *) = 4 sizeof(float *) = 4 sizeof(double *) = 4 sizeof (char * ) = 4 Pointers to different types are all the

same size. Now, back to my diagram.

Page 13: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px

memory

px

0F45AB24

2

6

4

0F45AB14

name address

Page 14: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x);

name address

Page 15: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

name address

Page 16: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

printf(“%d\n”,*px);

Page 17: Introduction to Pointers These Slides NOT From Text

Structure Of A Pointer

Symbol Table

X

Y

Z

0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C

X

Y

Z 0F45AB1C

0F45AB28

0F45AB14

int x,y,z;

int * px = &x;

px 0F45AB24

memory

2

6

4

px 0F45AB14

printf(“%d\n”,x); 2

printf(“%d\n”,*px);2

Page 18: Introduction to Pointers These Slides NOT From Text

Operators Related To Pointers

& address-of printf(“%x\n”,&variable);

* dereference printf(“%d\n”, * pointer_variable)

Page 19: Introduction to Pointers These Slides NOT From Text

Pointer Operator Syntax

thing Simple Thing (variable) &thing Pointer to variable thing thing_ptr Pointer to an integer *thing_ptr Integer

Page 20: Introduction to Pointers These Slides NOT From Text

Typical Uses Of Operators int thing; /* declare an integer (a thing) */ thing = 4; int * thing_ptr; /*declare a pointer to an

integer */ Don’t need _ptr, just helps readability. Doesn’t initialize to any value yet. (garbage)

thing_ptr = &thing; /* store address in pointer, points to thing */

Page 21: Introduction to Pointers These Slides NOT From Text

Cont.

*thing_ptr = 5; /* set thing to 5 */ *thing_ptr is a thing. thing_ptr is a pointer. Of course, thing_ptr could be set to

point to other variables besides thing, (it just wouldn’t be as understandable). thing_ptr = &something_else;

Page 22: Introduction to Pointers These Slides NOT From Text

Illegal and Strange Uses

*thing is illegal. thing is not a pointer variable.

&thing_ptr is legal, but strange. “a pointer to a pointer”

Page 23: Introduction to Pointers These Slides NOT From Text

Pointing to the Same Thing int something; int *first_ptr; /* one pointer */ int *second_ptr; /* another pointer */ something=1; first_ptr = &something; second_ptr = first_ptr;

something=1; *first_ptr=1; *second_ptr=1;

All identical results

(Only one integer)

Page 24: Introduction to Pointers These Slides NOT From Text

Pass By Reference

Pass the address of a variable by value to a function and dereference it inside the function.

i.e. Pass a pointer by value to a function and dereference it inside the function.

Page 25: Introduction to Pointers These Slides NOT From Text

Example

#include <stdio.h>

void inc_count(int *count_ptr)

{ (*count_ptr)++;}

int main(void)

{ int count=0;

while(count<10)

inc_count(&count);

return 0;

}

Page 26: Introduction to Pointers These Slides NOT From Text

Example

#include <stdio.h>

void inc_count(int *count_ptr)

{ (*count_ptr)++;}

int main(void)

{ int count=0;

while(count<10)

inc_count(&count);

return 0;

}

count

count_ptr

Page 27: Introduction to Pointers These Slides NOT From Text

Example

#include <stdio.h>

void inc_count(int *count_ptr)

{ (*count_ptr)++;}

int main(void)

{ int count=0;

while(count<10)

inc_count(&count);

return 0;

}

count

count_ptr

0

Page 28: Introduction to Pointers These Slides NOT From Text

Example

#include <stdio.h>

void inc_count(int *count_ptr)

{ (*count_ptr)++;}

int main(void)

{ int count=0;

while(count<10)

inc_count(&count);

return 0;

}

count

count_ptr

0 Address of

count passed

to function.

(received as

count_ptr)

Page 29: Introduction to Pointers These Slides NOT From Text

Example

#include <stdio.h>

void inc_count(int *count_ptr)

{ (*count_ptr)++;}

int main(void)

{ int count=0;

while(count<10)

inc_count(&count);

return 0;

}

count

count_ptr

0 Address of

count passed

to function.

(received as

count_ptr)

Increments the count variable in the main program.