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.
High Speed Memory (RAM)
1073741823
01
2345
1Gb of RAM
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
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.)
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”
Structure Of A Pointer
Symbol Table
0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C
int x=2,y=4,z=6;memory
Address shown in hex.
name address
Structure Of A Pointer
Symbol Table
X
Y
0F45AB140F45AB180F45AB1C0F45AB200F45AB240F45AB280F45AB2C Z
int x=2,y=4,z=6;memory
name address
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!
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
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
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.
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
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
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
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);
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
Operators Related To Pointers
& address-of printf(“%x\n”,&variable);
* dereference printf(“%d\n”, * pointer_variable)
Pointer Operator Syntax
thing Simple Thing (variable) &thing Pointer to variable thing thing_ptr Pointer to an integer *thing_ptr Integer
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 */
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;
Illegal and Strange Uses
*thing is illegal. thing is not a pointer variable.
&thing_ptr is legal, but strange. “a pointer to a pointer”
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)
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.
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;
}
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
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
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)
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.
Top Related