contents of what a pointer points to??

Discussion in 'C Programming' started by JS, Mar 16, 2005.

  1. JS

    JS Guest

    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
    JS, Mar 16, 2005
    #1
    1. Advertising

  2. JS

    Mark Odell Guest

    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.
    Mark Odell, Mar 16, 2005
    #2
    1. Advertising

  3. JS

    bjrnove Guest

    > 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
    bjrnove, Mar 16, 2005
    #3
  4. JS

    JS Guest

    "Mark Odell" <> skrev i en meddelelse
    news:...
    > 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
    JS, Mar 16, 2005
    #4
  5. JS

    Mark Odell Guest

    JS wrote:
    > "Mark Odell" <> skrev i en meddelelse
    > news:...
    >
    >>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
    Mark Odell, Mar 16, 2005
    #5
  6. 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
    Lawrence Kirby, Mar 16, 2005
    #6
  7. JS

    JS Guest

    "Mark Odell" <> skrev i en meddelelse
    news:...
    > JS wrote:
    > > "Mark Odell" <> skrev i en meddelelse
    > > news:...
    > >
    > >>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?
    JS, Mar 16, 2005
    #7
  8. JS

    Mark Odell Guest

    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
    Mark Odell, Mar 16, 2005
    #8
  9. JS

    JS Guest

    "Lawrence Kirby" <> skrev i en meddelelse
    news:p...
    > 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;
    JS, Mar 16, 2005
    #9
  10. JS

    Ben Pfaff Guest

    "JS" <> 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
    Ben Pfaff, Mar 16, 2005
    #10
  11. "JS" <> writes:
    > "Lawrence Kirby" <> skrev i en meddelelse
    > news:p...
    >> 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;

    [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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 16, 2005
    #11
  12. JS

    Flash Gordon Guest

    JS wrote:

    <snip>

    > 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.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Mar 16, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James.D
    Replies:
    6
    Views:
    406
    James.D
    Mar 5, 2004
  2. Levent

    type that pointer points to

    Levent, May 5, 2005, in forum: C++
    Replies:
    3
    Views:
    484
    Levent
    May 5, 2005
  3. R.kumaran
    Replies:
    6
    Views:
    399
    Kevin Easton
    Sep 3, 2003
  4. Andrew

    Test if pointer points to allocated memory

    Andrew, Jan 21, 2004, in forum: C Programming
    Replies:
    34
    Views:
    940
    Daniel Haude
    Jan 26, 2004
  5. G
    Replies:
    3
    Views:
    438
    Sylvester Hesp
    Jan 8, 2007
Loading...

Share This Page