Beruflich Dokumente
Kultur Dokumente
:
:
|----|
| 151|
|----|
|5100|
|----|
|
?|
|----|
:
:
0x5108
0x5104
0x5100
Address:
p = &n;
n = 151;
Address:
:
:
|-----|
0x5100|
|
|-----|
0x5104|
|
|-----|
0x5108|
|
|-----|
:
:
q is a pointer: 1 word
p is a pointer: 1 word
char: 1 byte
char: 1 byte
char: 1 byte
char: 1 byte
q = name;
++p;
We set p to the value of name. Now p has value hex 5100 too.
We can use the * dereferencing operator on p, and get the
character 'B' as a result.
p = name;
|---|
0x5100 |'B'|
|---|
0x5101 |'i'|
|---|
0x5102 |'l'|
|---|
0x5103 |'l'|
|---|
0x5104 |\0 |
|---|
0x5105 |
|
|---|
0x5109 |
|
|---|
main()
{
char name[] = "Bill";
char *p;
int *q;
main()
{
int n;
int *p;
The & operator says, "tell me the address where the following object
starts." In this case, it is hex 5100 (I put a '$' before it, to
conform to the Assembly notation I am used to).
Notice the _value_ of p is an address.
Pgina 1 de 4
Pgina 2 de 4
printf("%d.\n", x);
n = 151;
f(n);
x is another integer
n is an integer
printf("%d.\n", *x);
*x = 451;
n = 151;
f(&n);
|----|
0x5100 | 151|
|----|
0x5104 |5100|
|----|
x is a pointer to int
n is an integer
f(x)
int *x;
{
main()
{
int n;
But what if we want to have f() modify the value and then
have that new value be available in main()? C does this by
passing the variable "by reference".
|---|
0x5100 |151|
|---|
0x5104 |151|
|---|
f(x)
int x;
{
main()
{
int n;
number.
Now print the dereferenced value of q (i.e. the value of the object
q is pointing to). Well, it's pointing at a null byte, and then
the first 3 bytes of the char *p. Now we're all messed up.
Nice going. Try to convert _that_ to an integer representation.
Well actually, C will do it happily. But it'll be another weird
++q;
Pgina 3 de 4
x is a pointer to int
n is an integer
no variable name.
here is x, a pointer.
Or, "pointer to pointer to int, and by the way, after we're done,
p has been incremented. But we looked where it was pointing
before it got incremented, so we don't care. Let the next statement
worry about it."
And what does "**p++" mean? Well, ++ binds tighter than *, so this
is equivalent to: *( *( p++ ) )
Now "*x = **p" looks like, "this int at 5100 gets the value of
that int at 5104."
And you know what "*p" means, "the value of location 5108".
Now that value is another address! Okay, let's dereference that
address: "**p" and we find (by the declaration) an int.
|----|
0x5104 | 12|
|----|
0x5108 |5104|
|----|
0x511c |5108|
|----|
0x5120 |5100|
|----|
|----|
0x5100 |
0|
|----|
0x5100 | 451|
|----|
0x5104 |5100|
|----|
Pgina 4 de 4