Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > contents of what a pointer points to??

Reply
Thread Tools

contents of what a pointer points to??

 
 
JS
Guest
Posts: n/a
 
      03-16-2005
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


 
Reply With Quote
 
 
 
 
Mark Odell
Guest
Posts: n/a
 
      03-16-2005
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.
 
Reply With Quote
 
 
 
 
bjrnove
Guest
Posts: n/a
 
      03-16-2005
> 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.

--
bjrnove

 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-16-2005

"Mark Odell" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> 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.

JS


 
Reply With Quote
 
Mark Odell
Guest
Posts: n/a
 
      03-16-2005
JS wrote:
> "Mark Odell" <(E-Mail Removed)> skrev i en meddelelse
> news:(E-Mail Removed)...
>
>>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 */

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

*pAllocbuf = allocbuf; /* Error! */

Okay?

- Mark
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      03-16-2005
On Wed, 16 Mar 2005 15:52:23 +0100, 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"??


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
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-16-2005

"Mark Odell" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> JS wrote:
> > "Mark Odell" <(E-Mail Removed)> skrev i en meddelelse
> > news:(E-Mail Removed)...
> >
> >>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?


 
Reply With Quote
 
Mark Odell
Guest
Posts: n/a
 
      03-16-2005
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?


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

--
- Mark
 
Reply With Quote
 
JS
Guest
Posts: n/a
 
      03-16-2005

"Lawrence Kirby" <(E-Mail Removed)> skrev i en meddelelse
news(E-Mail Removed) k...
> On Wed, 16 Mar 2005 15:52:23 +0100, 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"??

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


 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-16-2005
"JS" <(E-Mail Removed)> writes:

> 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.
--
"In My Egotistical Opinion, most people's C programs should be indented six
feet downward and covered with dirt." -- Blair P. Houghton
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
evaluate NULL to a pointer which a CONST pointer points to G C++ 3 01-08-2007 04:22 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments