Is this assignment ok?

Discussion in 'C Programming' started by Lew Pitcher, Aug 19, 2003.

  1. Lew Pitcher

    Lew Pitcher Guest

    Colin JN Breame wrote:

    > char *pA[5];
    > char *pB[5];
    >
    > pA = pB;
    >
    > This doesnt compile.


    Not surprisingly.

    pA is the name of an array (an array of 5 pointers to char)
    pB is the name of an array (an array of 5 pointers to char)

    Neither pA nor pB is alterable, although their /contents/ are.

    > On second thoughts, does
    > (x) char *pA[5] mean
    > (a) "A pointer to 5 characters" or
    > (b) "5 pointers to a character" ?


    (b)

    > If (x) == (b) then how would (a) be declared?


    char (*pA)[5];

    > Any help would be appreciated.



    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
     
    Lew Pitcher, Aug 19, 2003
    #1
    1. Advertising

  2. In article <bht5f0$9ao$>,
    Joona I Palaste <> wrote:
    >Colin JN Breame <> scribbled the following:
    >> (a) "A pointer to 5 characters" or

    >
    >> If (x) == (b) then how would (a) be declared?

    >
    >char (*pB)[5];


    That's a pointer to (and array of) five pointers to characters.

    A pointer to (an array of) 5 characters would be:
    char *p[5];

    -- Brett
     
    Brett Frankenberger, Aug 19, 2003
    #2
    1. Advertising

  3. >> On second thoughts, does
    >> (x) char *pA[5] mean


    is _roughly_ equivalent to
    *pa0 *pa1 *pa2 *pa3 *pa4
    (see argv hint hint!)

    >> (a) "A pointer to 5 characters" or
    >> (b) "5 pointers to a character" ?

    >
    >It means 5 pointers to a character.


    For me this is 5 pointers to a string (┬┤aka "character array")

    >> If (x) == (b) then how would (a) be declared?

    >
    >char (*pB)[5];


    Because IMO * takes precende before [] (does it? please help if I am incorrect)
    I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]

    --
    - Jan Engelhardt
     
    Jan Engelhardt, Aug 19, 2003
    #3
  4. char *pA[5];
    char *pB[5];

    pA = pB;

    This doesnt compile.

    On second thoughts, does
    (x) char *pA[5] mean
    (a) "A pointer to 5 characters" or
    (b) "5 pointers to a character" ?

    If (x) == (b) then how would (a) be declared?

    Any help would be appreciated.
     
    Colin JN Breame, Aug 19, 2003
    #4
  5. Brett Frankenberger <> scribbled the following:
    > In article <bht5f0$9ao$>,
    > Joona I Palaste <> wrote:
    >>Colin JN Breame <> scribbled the following:
    >>> (a) "A pointer to 5 characters" or

    >>
    >>> If (x) == (b) then how would (a) be declared?

    >>
    >>char (*pB)[5];


    > That's a pointer to (and array of) five pointers to characters.


    No, that's a pointer to an array of 5 characters.

    > A pointer to (an array of) 5 characters would be:
    > char *p[5];


    No, that would make p an array of 5 pointers to a character.
    For example:
    char *p[5] = {"a", "b", "c", "d", "e"};

    At least I *think* this is true. Feel free to correct me if you *know*
    otherwise.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "The truth is out there, man! Way out there!"
    - Professor Ashfield
     
    Joona I Palaste, Aug 19, 2003
    #5
  6. Jan Engelhardt <> scribbled the following:
    >>> If (x) == (b) then how would (a) be declared?

    >>
    >>char (*pB)[5];


    > Because IMO * takes precende before [] (does it? please help if I am incorrect)
    > I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]


    It's a pointer to an array of 5 chars. You are right that * takes
    precedence over [], but parantheses ( and ) are a way of side-stepping
    precedence. (*pb)[5] is not at all equivalent to *pb[5], any more than
    1+2*3 is equivalent to (1+2)*3. (The first has value 7, the second has
    value 9.)

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Insanity is to be shared."
    - Tailgunner
     
    Joona I Palaste, Aug 19, 2003
    #6
  7. >>>> (a) "A pointer to 5 characters" or
    >>>> If (x) == (b) then how would (a) be declared?
    >>>char (*pB)[5];

    >> That's a pointer to (and array of) five pointers to characters.

    >No, that's a pointer to an array of 5 characters.
    >
    >> A pointer to (an array of) 5 characters would be:
    >> char *p[5];

    >
    >No, that would make p an array of 5 pointers to a character.
    >For example:
    >char *p[5] = {"a", "b", "c", "d", "e"};


    Heh that's ... like reviceversa blah.
    You say it's 5 pointers to *a* character, though you supply constant strings
    (heh type "char *").

    >At least I *think* this is true. Feel free to correct me if you *know*
    >otherwise.


    So as you can see yourself, you just declared "an array which holds 5 char*
    things". To clearify:
    typeof(p[0]) is "char *",
    so your statment "5 pointers to a characters" cannot really hold true.

    --
    - Jan Engelhardt
     
    Jan Engelhardt, Aug 19, 2003
    #7
  8. Jan Engelhardt <> scribbled the following:
    >>>>> (a) "A pointer to 5 characters" or
    >>>>> If (x) == (b) then how would (a) be declared?
    >>>>char (*pB)[5];
    >>> That's a pointer to (and array of) five pointers to characters.

    >>No, that's a pointer to an array of 5 characters.
    >>
    >>> A pointer to (an array of) 5 characters would be:
    >>> char *p[5];

    >>
    >>No, that would make p an array of 5 pointers to a character.
    >>For example:
    >>char *p[5] = {"a", "b", "c", "d", "e"};


    > Heh that's ... like reviceversa blah.
    > You say it's 5 pointers to *a* character, though you supply constant strings
    > (heh type "char *").


    The pointers point to the first characters of the constant strings,
    in other words, to the characters 'a', 'b', 'c', 'd' and 'e'
    respectively. That's how C strings work.

    >>At least I *think* this is true. Feel free to correct me if you *know*
    >>otherwise.


    > So as you can see yourself, you just declared "an array which holds 5 char*
    > things". To clearify:
    > typeof(p[0]) is "char *",
    > so your statment "5 pointers to a characters" cannot really hold true.


    You appear to contradict yourself. If typeof(p[0]) is "char *" (which
    is true), then p[0] is a pointer to a character, right? What would this
    make p then, if not an array of pointers to a character?

    Let me reiterate.
    char *p[5];
    p[0] is a pointer to a character.
    p is an array of pointers to a character. (Or an array of pointers to
    characters, if you want.)

    On the other hand:
    char (*p)[5];
    *p is an array of characters. (5 characters, to be precise.)
    p is a pointer to an array of characters (an array of 5 characters).

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "To know me IS to love me."
    - JIPsoft
     
    Joona I Palaste, Aug 19, 2003
    #8
  9. Lew Pitcher

    Al Bowers Guest

    Jan Engelhardt wrote:

    >
    > Because IMO * takes precende before [] (does it? please help if I am incorrect)
    > I say it's 5 ptrs to "char*" and thus (*pb)[5] is equivalent to *pb[5]
    >


    No. The [] operator has precedence over the * operator. Thus, they are
    not equivalent.

    I find it handy to have easy access to a Table of Precedence and
    Associativity of Operators instead of depending on memory.

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Aug 19, 2003
    #9
  10. Colin JN Breame wrote:

    > char *pA[5];
    > char *pB[5];
    >
    > pA = pB;
    >
    > This doesnt compile.
    >
    > On second thoughts, does
    > (x) char *pA[5] mean
    > (a) "A pointer to 5 characters" or
    > (b) "5 pointers to a character" ?
    >
    > If (x) == (b) then how would (a) be declared?
    >
    > Any help would be appreciated.


    By the way, a pointer to 5 characters is the
    same as a pointer to a single character.
    char letters[5] = {'a', 'b', 'c', 'd', 'e'};
    char * p;
    p = letters; /* p now points to 5 letters. */

    The variable 'p' above actually points to the
    first of 5 letters. A pointer to 5 characters
    will also point to the first of 5 characters.

    Unless your being type specific, I would lose
    the "pointer to 5 characters" and just use
    a pointer to a character.

    Another reason not to use a "pointer to 5
    characters" is that the C language has no
    automatic boundary checking on arrays. There is
    nothing preventing you from pointing to the
    fifth character then incrementing the pointer.
    Dereferencing the pointer will lead to undefined
    behavior, but still, there is no run-time check
    to see that the pointer has been incremented past
    the end of the array.

    --
    Thomas Matthews
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
     
    Thomas Matthews, Aug 19, 2003
    #10
  11. Lew Pitcher

    Neil Cerutti Guest

    In article <bhtblt$2ua9e$-berlin.de>, Al
    Bowers wrote:
    > Jan Engelhardt wrote:
    >> Because IMO * takes precende before [] (does it? please help
    >> if I am incorrect) I say it's 5 ptrs to "char*" and thus
    >> (*pb)[5] is equivalent to *pb[5]

    >
    > No. The [] operator has precedence over the * operator. Thus,
    > they are not equivalent.
    >
    > I find it handy to have easy access to a Table of Precedence
    > and Associativity of Operators instead of depending on memory.


    Except in declarations they are not operators, they're
    declarators. ;-)

    --
    Neil Cerutti
     
    Neil Cerutti, Aug 19, 2003
    #11
  12. Lew Pitcher

    bd Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Colin JN Breame wrote:

    > char *pA[5];
    > char *pB[5];
    >
    > pA = pB;
    >
    > This doesnt compile.
    >
    > On second thoughts, does
    > (x) char *pA[5] mean
    > (a) "A pointer to 5 characters" or


    No. That'd be:
    char *pA;
    Then you'd point it to the first of those 5 characters, like so:
    char array[5];
    char *pA = array;
    pA[3] = 42;

    Or, to use dynamically allocated memory:
    char *pA;
    pA = malloc(sizeof *pa * 5);
    if(!pA){
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(EXIT_FAILURE);
    }
    pA[3] = 42;
    /* ... */
    free(pA);
    pA = NULL; /* Often helps catch use of free()d memory */

    This works because, in C, a is the same as *(a + b), and addition of a
    pointer and an integer works in multiples of the size of the pointed-to
    type.

    > (b) "5 pointers to a character" ?

    Correct.

    It automatically decays to a pointer to its first element, but that's not a
    lvalue, so you can't assign to it. Try this:
    memcpy(pA, pB, sizeof pA);


    - --
    Freenet distribution not available
    "Yes, it's the right planet, all right, " he said again.
    "Right planet, wrong universe. "

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQCgu3bG
    qvXZk1Cx6l1rb29MvbqP00A=
    =HRO9
    -----END PGP SIGNATURE-----
     
    bd, Aug 19, 2003
    #12
  13. Lew Pitcher

    Mike Wahler Guest

    Colin JN Breame <> wrote in message
    news:p...
    > char *pA[5];
    > char *pB[5];
    >
    > pA = pB;
    >
    > This doesnt compile.


    Of course not. You cannot assign to an array.
    Each element must be assigned individually.

    for(size_t i = 0; i < sizeof pA / sizeof *pA; ++i)
    pA = pB;

    >
    > On second thoughts, does
    > (x) char *pA[5] mean
    > (a) "A pointer to 5 characters" or
    > (b) "5 pointers to a character" ?


    Array of five objects of type 'pointer to char'.

    >
    > If (x) == (b) then how would (a) be declared?


    Pointer to array of five characters:

    char (*arr)[5];

    An array of five characters:

    char arr[5];

    Pointer to any character, be it an array element
    or not:

    char *p;

    Assign to 'p' the address of the first element of
    array:

    p = arr;


    >
    > Any help would be appreciated.


    Which C book(s) are you studying?

    -Mike
     
    Mike Wahler, Aug 19, 2003
    #13
  14. Lew Pitcher

    Mike Wahler Guest

    Brett Frankenberger <> wrote in message
    news:bht8qi$mil$...
    > In article <bht5f0$9ao$>,
    > Joona I Palaste <> wrote:
    > >Colin JN Breame <> scribbled the following:
    > >> (a) "A pointer to 5 characters" or

    > >
    > >> If (x) == (b) then how would (a) be declared?

    > >
    > >char (*pB)[5];

    >
    > That's a pointer to (and array of) five pointers to characters.
    >
    > A pointer to (an array of) 5 characters would be:
    > char *p[5];


    BZZT!!

    Try again.

    -Mike
     
    Mike Wahler, Aug 19, 2003
    #14
  15. Lew Pitcher

    Jeff Guest

    "bd" <> wrote in message
    news:-ip.org...
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Colin JN Breame wrote:
    >
    > > char *pA[5];
    > > char *pB[5];
    > >
    > > pA = pB;
    > >
    > > This doesnt compile.
    > >
    > > On second thoughts, does
    > > (x) char *pA[5] mean
    > > (a) "A pointer to 5 characters" or

    >
    > No. That'd be:
    > char *pA;
    > Then you'd point it to the first of those 5 characters, like so:
    > char array[5];
    > char *pA = array;
    > pA[3] = 42;
    >
    > Or, to use dynamically allocated memory:
    > char *pA;
    > pA = malloc(sizeof *pa * 5);


    I think the 'sizeof' is not needed here. The size of char is defined as 1
    byte.

    pA = malloc(5);


    > if(!pA){
    > fprintf(stderr, "Unable to allocate memory.\n");
    > exit(EXIT_FAILURE);
    > }
    > pA[3] = 42;
    > /* ... */
    > free(pA);
    > pA = NULL; /* Often helps catch use of free()d memory */
    >
    > This works because, in C, a is the same as *(a + b), and addition of a
    > pointer and an integer works in multiples of the size of the pointed-to
    > type.
    >
    > > (b) "5 pointers to a character" ?

    > Correct.
    >
    > It automatically decays to a pointer to its first element, but that's not

    a
    > lvalue, so you can't assign to it. Try this:
    > memcpy(pA, pB, sizeof pA);
    >
    >
    > - --
    > Freenet distribution not available
    > "Yes, it's the right planet, all right, " he said again.
    > "Right planet, wrong universe. "
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.2 (GNU/Linux)
    >
    > iD8DBQE/Qousx533NjVSos4RAlY2AJ9BxTiSTrXQotMvqCd0vXeLRZMrEQCgu3bG
    > qvXZk1Cx6l1rb29MvbqP00A=
    > =HRO9
    > -----END PGP SIGNATURE-----


    --
    Jeff
     
    Jeff, Aug 20, 2003
    #15
  16. >
    > Which C book(s) are you studying?
    >

    I have a copy of K&R but dont often find it very useful.
     
    Colin JN Breame, Aug 20, 2003
    #16
  17. Lew Pitcher

    Default User Guest

    Colin JN Breame wrote:
    >
    > >
    > > Which C book(s) are you studying?
    > >

    > I have a copy of K&R but dont often find it very useful.


    Most of the experienced programmers here find that to be very useful.
    That should tell you something.




    Brian Rodenborn
     
    Default User, Aug 20, 2003
    #17
    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. itsme
    Replies:
    1
    Views:
    1,680
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,510
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. John Potter

    Question: inout signal assignment

    John Potter, Sep 6, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,786
    Renaud Pacalet
    Sep 8, 2003
  4. nagy
    Replies:
    36
    Views:
    1,041
    Terry Reedy
    Jul 20, 2006
  5. Chris
    Replies:
    34
    Views:
    1,567
Loading...

Share This Page