contents of what a pointer points to??

J

JS

In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"??

JS
 
M

Mark Odell

JS said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"??

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.
 
B

bjrnove

static char *allocp = allocbuf;
^^^^^^^^^
If you look more careful you'll see that allocp has been set to the
start of the array allocbuf. allocbuf is basicly a pointer to the
memorylocation for the array.
 
J

JS

Mark Odell said:
JS said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"??

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.

But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is
the first element of allocbuf.

JS
 
M

Mark Odell

JS said:
JS said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it
possible
to make the contents of an unknown address be "allocbuf"??

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.


But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is
the first element of allocbuf.

Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */

Do not confuse this with assignment (not the same as initialization)
like this:

*pAllocbuf = allocbuf; /* Error! */

Okay?

- Mark
 
L

Lawrence Kirby

In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"??

Your second line has an initialiser for allocp. It is saying that allocp
is a static variable of type char * and its initial value is (a
pointer to the first element of) allocbuf. The initialisation is broadly
equivalent to the assignment (executed sometime before the program starts):

allocp = allocbuf;

it is NOT equivalent to

*allocp = allocbuf;

Lawrence
 
J

JS

Mark Odell said:
JS said:
JS wrote:

In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it
possible

to make the contents of an unknown address be "allocbuf"??

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.


But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at is
the first element of allocbuf.

Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */

This last line implies pAllocbuf = allocbuf..right?

But is it the value at allocbuf[0] or is it the address?
 
M

Mark Odell

JS said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it

possible


to make the contents of an unknown address be "allocbuf"??

It has been set to point to something, allocbuf[] which is a block of
memory. So allocp points to the first element of allocbuf.


But should that not be written as:

1) static char *allocp; // declares that *allocp is a pointer to a char.
2) allocp = &allocbuf[0]; // makes allocp point to the first address in
allocbuf.
3) allocp = allocbuf; // the same as 2) just a short hand.

Then I can write:

*allocp = allocbuf;

which means that the contents of the address that allocp is pointing at
is
the first element of allocbuf.

Can be. There is a convenient shortcut you can take during definition,
that is, you can initialze at the definition point. E.g.

int value = 12;
int *pValue = &value;

char allocbuf[1024];
char *pAllocbuf = allocbuf; /* or &allocbuf[0] if you wish */


This last line implies pAllocbuf = allocbuf..right?

It doesn't imply it, C says that it will initialize pAllocbuf to point
to allocbuf just as if you had written:

pAllocbuf = allocbuf;

So I guess your statement is correct.
But is it the value at allocbuf[0] or is it the address?

pAllocbuf will contain the address of allocbuf[0], e.g. &allocbuf[0] and
*pAllocbuf will contain the value held in allocbuf[0].
 
J

JS

Lawrence Kirby said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;

But *allocp has not been set to point at anything yet so how is it possible
to make the contents of an unknown address be "allocbuf"??

Your second line has an initialiser for allocp. It is saying that allocp
is a static variable of type char * and its initial value is (a
pointer to the first element of) allocbuf. The initialisation is broadly
equivalent to the assignment (executed sometime before the program starts):

allocp = allocbuf;

it is NOT equivalent to

*allocp = allocbuf;


In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];



with I assume is also equivalent with:

static char allocp = allocbuf;
 
B

Ben Pfaff

JS said:
In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];

Yes. If allocbuf is an array of char or a pointer to char, both
are valid and equivalent.
with I assume is also equivalent with:

static char allocp = allocbuf;

No. Given the same assumption, this code requires a diagnostic
because it implicitly converts from a pointer type to a character
type.
 
K

Keith Thompson

JS said:
Lawrence Kirby said:
In K&R I have found this:

static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;
[snip]
allocp = allocbuf;

it is NOT equivalent to

*allocp = allocbuf;


In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];
Yes.

with I assume is also equivalent with:

static char allocp = allocbuf;

No, that's not equivalent at all. The first two declarations declare
allocp as a variable of type pointer-to-char. The last declares is as
a variable of type char, a completely different type.

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/faq.html>.
I recommend section 6, which discusses arrays and pointers. (Actually
I recommend the whole thing, but section 6 is most relevant to what
you're asking about.)
 
F

Flash Gordon

JS wrote:

In the book they say that:

static char *allocp = allocbuf;

is equivalent with:

static char *allocp = &allocbuf[0];

Yes. That is because the name of an array degenerates to a pointer to
it's first element and, obviously, the address of the first element is a
pointer to the first element.
with I assume is also equivalent with:

static char allocp = allocbuf;

Why would you assume that? "char *allocp" is a pointer to char, "char
allocp" is a char. Whether you initialise it at the same time has
nothing to do with it. So

static char allocp = allocbuf;

Is an error when allocbuf is an array.

I suggest you reread more carefully what your book says about declaring
variables and pointer and then reread what it says about initialisers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top