Sie sind auf Seite 1von 2

2:44am

other unused memory

p is a pointer, also one word big

n is an integer, one machine word big

:
:
|----|
| 151|
|----|
|5100|
|----|
|
?|
|----|
:
:

Value at that address:

Now I want to print out the memory address of n.

The * operator says, "give me the object at the following address."


The object's type is the type that the pointer was declared as.
So, since we declared "int *p", the object pointed at will be
_assumed_ by C to be an int. In this case, we were careful to
make this coincide with what we were pointing at.

printf("n is %d.\n", n);


printf("n is %d.\n", *p);

Now I want to print out the value of n, by two ways.

0x5108

0x5104

0x5100

Address:

That says, "the value of the variable p is assigned the


address of the variable n".

p = &n;

I set the pointer p to point to the integer n.

n = 151;

Let's give these variables some values.


I set n to be the number 151.

Address:

:
:
|-----|
0x5100|
|
|-----|
0x5104|
|
|-----|
0x5108|
|
|-----|
:
:

There is a place in my memory that looks like this:

Here's how memory looks now:

q is a pointer: 1 word

p is a pointer: 1 word

char: 1 byte

char: 1 byte

char: 1 byte

char: 1 byte

"name" is an address constant that has value hex 5100

What value does it have now?

What we have just seen here is a key issue of pointers that I


mentioned earlier: C assumes that what they are pointing at
is an object of the type that the pointer was designed to point at.
It is up to the programmer to make sure this happens correctly.

But q is a pointer to int! If we dereference q, it will take


the word (typically 4 bytes) beginning at address "name" (which
is hex 5100) and try to convert it to an int. 'B', 'i', 'l', 'l'
converted to an int will be some large number, dependant on the
bit-ordering algorithm on your machine's architecture. On ucscb,
it becomes 1114205292. (to see how, line up the binary representation
of the ascii values for those 4 characters, and then run the 32 bits
together, and convert that resultant binary number as an integer.)

q = name;

Now let's try something irresponsible:

The pointer p is incremented.


Hex 5101. Pretty simple.

++p;

Now what happens if I do this:

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 |
|
|---|

Now we have an array to play with.

main()
{
char name[] = "Bill";
char *p;
int *q;

using the & operator.

main()
{
int n;
int *p;

printf("p is located at $%x.\n", &p);

Hm. Does p have an address? Sure. It is a variable, and all


variables have their own address. The address of p is hex 5104.

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.

printf("n is located at $%x.\n", &n);


printf("n is located at $%x.\n", p);

Archivo: Documento sin ttulo 1

Here we are taking the address of a pointer variable,

Pgina 1 de 4

When I have some code:

Well, if you know assembly, you have a head start


on many of the cis freshpersons here. You at least know
about memory maps: RAM is a long long array of bytes.
It helped me to learn about pointers if I kept this in mind.
For some reason, books and instructors talking about
pointers want to overlook this.

Message 1956 (8 left): Thu Jan 25 1990


From: Bill! (easterb@ucscb)
Subject: Okay

Archivo: Documento sin ttulo 1

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

Pass the _address_ of n, and declare x as a _pointer_ to int.


Actually, this is still passing by value, but the value being
passed is the address, not the number.

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".

When we mention x, we are using the value at location 5104,


and we can change it, read it, whatever, and it won't affect n,
the int at location 5100.

|---|
0x5100 |151|
|---|
0x5104 |151|
|---|

Here is a simple program that passes an int "by value".


That is, it copies the value of n into the new variable x!

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

The int pointer is incremented. What value does it have now?


Hex 5104. Huh?!? The answer is simple if you accept the above
paragraph. It gets incremented by the size of the object it
_thinks_ it is pointing at. It's an int pointer, so incrementing
it makes it advance a number of bytes equal to the size of an int.

++q;

Archivo: Documento sin ttulo 1

Pgina 3 de 4

x is a pointer to int

n is an integer

no variable name.

here is x, a pointer.

guess where it's pointing.

here is p, a pointer to int pointer.

Here is an int pointer, pointing at the previous word.

here is a value, a word in memory.

here is a word in memory with initial value 0.


no variable name

It means, "the value of location 5100."

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.

You should know now what "*x" means.

The subordinate pointer is a pointer to int.*/

First let's see what p and x were declared as:


int *x;
/* pointer to int */
int **p;
/* pointer to pointer.

|----|
0x5104 | 12|
|----|
0x5108 |5104|
|----|
0x511c |5108|
|----|
0x5120 |5100|
|----|

|----|
0x5100 |
0|

Well, those are the basics.


You mentioned things like "*x=**p++" being ugly and unreadable.
Well, yeah, but here is a diagram that may help:

x still points to location 5100, but we have changed the value


of the object at that location.

|----|
0x5100 | 451|
|----|
0x5104 |5100|
|----|

Now if f() when we make use of *x, we are referring to the


value at location 5100. This is the location of n.
After the assignment "*x = 451;", this is what we have:

Archivo: Documento sin ttulo 1

Pgina 4 de 4

Das könnte Ihnen auch gefallen