Download presentation
Presentation is loading. Please wait.
1
Introduction to Pointers These Slides NOT From Text
2
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.
3
High Speed Memory (RAM) 1073741823 0 1 2 3 4 5 1Gb of RAM
4
So What Is The Size Of Different Types? printf(“sizeof(int) = %d\n”, sizeof(int)); I ran a short program with a bunch of these statements. 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
5
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.)
6
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”
7
Structure Of A Pointer Symbol Table 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C int x=2,y=4,z=6; memory Address shown in hex. name address
8
Structure Of A Pointer Symbol Table X Y 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C Z int x=2,y=4,z=6; memory name address
9
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 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!
10
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x=2,y=4,z=6; int * px = &x; memory 2 6 4 name address
11
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px memory px 0F45AB24 2 6 4 0F45AB14 name address
12
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.
13
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px memory px 0F45AB24 2 6 4 0F45AB14 name address
14
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x); name address
15
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 0F45AB28 0F45AB14 int x,y,z; int * px = &x; px 0F45AB24 memory 2 6 4 px 0F45AB14 printf(“%d\n”,x);2 name address
16
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 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);
17
Structure Of A Pointer Symbol Table X Y Z 0F45AB14 0F45AB18 0F45AB1C 0F45AB20 0F45AB24 0F45AB28 0F45AB2C X Y Z0F45AB1C 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
18
Operators Related To Pointers & address-of printf(“%x\n”,&variable); * dereference printf(“%d\n”, * pointer_variable)
19
Pointer Operator Syntax thing Simple Thing (variable) &thingPointer to variable thing thing_ptr Pointer to an integer *thing_ptr Integer
20
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 */
21
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;
22
Illegal and Strange Uses *thing is illegal. thing is not a pointer variable. &thing_ptr is legal, but strange. “a pointer to a pointer”
23
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)
24
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.
25
Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; }
26
Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr
27
Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0
28
Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0 Address of count passed to function. (received as count_ptr)
29
Example #include void inc_count(int *count_ptr) { (*count_ptr)++;} int main(void) { int count=0; while(count<10) inc_count(&count); return 0; } countcount_ptr 0 Address of count passed to function. (received as count_ptr) Increments the count variable in the main program.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.