introduction to pointers these slides not from text
Post on 19-Dec-2015
228 views
TRANSCRIPT
![Page 1: Introduction to Pointers These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/1.jpg)
Introduction to Pointers
These Slides NOT From Text
![Page 2: Introduction to Pointers These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/3.jpg)
High Speed Memory (RAM)
1073741823
01
2345
1Gb of RAM
![Page 4: Introduction to Pointers These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/24.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/26.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/27.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062304/56649d355503460f94a0d09c/html5/thumbnails/29.jpg)
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.