const char problem

Discussion in 'C Programming' started by sugaray, Mar 3, 2004.

  1. sugaray

    sugaray Guest

    I wrote a short code as shown below for experiment purpose,
    It is successfully compilable, yet I found the piece I wrote
    even confused myself with the consts and the asterisks, and
    which brought up three questions to me:

    1) will the casting in line 1 affect the rest of the
    declarations ?
    2) what's the difference between line 2 and line 3
    3) are there any practical purposes of the usage of any
    of the three ?

    #include <stdio.h>

    int main(int argc,char **argv)
    {

    const char **pa=(const char **)argv; // line 1
    const char *const *p=argv; // line 2
    const char *const *const pp=argv; // line 3

    printf("%s %c\n",*++p,**p);
    printf("%s %c\n",*pp,**pp);

    return 0;
    }
     
    sugaray, Mar 3, 2004
    #1
    1. Advertising

  2. sugaray

    pete Guest

    sugaray wrote:
    >
    > I wrote a short code as shown below for experiment purpose,
    > It is successfully compilable, yet I found the piece I wrote
    > even confused myself with the consts and the asterisks, and
    > which brought up three questions to me:
    >
    > 1) will the casting in line 1 affect the rest of the
    > declarations ?


    No, not even if the cast had any effect at all, which it doesn't.

    > 2) what's the difference between line 2 and line 3


    pp has a const qulaifier that p doesn't have.

    > 3) are there any practical purposes of the usage of any
    > of the three ?
    >
    > #include <stdio.h>
    >
    > int main(int argc,char **argv)
    > {
    >
    > const char **pa=(const char **)argv; // line 1
    > const char *const *p=argv; // line 2
    > const char *const *const pp=argv; // line 3
    >
    > printf("%s %c\n",*++p,**p);


    The above line is unspecified behavior
    depending on which argument is evaluated first.

    > printf("%s %c\n",*pp,**pp);
    >
    > return 0;
    > }


    --
    pete
     
    pete, Mar 3, 2004
    #2
    1. Advertising

  3. sugaray

    Dan Pop Guest

    In <> pete <> writes:

    >> printf("%s %c\n",*++p,**p);

    >
    >The above line is unspecified behavior
    >depending on which argument is evaluated first.


    Nope, it's undefined behaviour, because the old value of p is used for
    other purposes than computing its new value, with no intervening
    sequence point.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Mar 3, 2004
    #3
  4. sugaray

    Richard Bos Guest

    pete <> wrote:

    > sugaray wrote:
    >
    > > printf("%s %c\n",*++p,**p);

    >
    > The above line is unspecified behavior
    > depending on which argument is evaluated first.


    Undefined. p gets read and modified without a sequence point in between.

    Richard
     
    Richard Bos, Mar 3, 2004
    #4
  5. sugaray

    Dan Pop Guest

    In <> (Richard Bos) writes:

    >pete <> wrote:
    >
    >> sugaray wrote:
    >>
    >> > printf("%s %c\n",*++p,**p);

    >>
    >> The above line is unspecified behavior
    >> depending on which argument is evaluated first.

    >
    >Undefined. p gets read and modified without a sequence point in between.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    If that were enough for invoking undefined behaviour, i = i + 1 would
    invoke undefined behaviour. The actual rule is slightly more complex.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Mar 3, 2004
    #5
  6. sugaray

    pete Guest

    Dan Pop wrote:
    >
    > In <> pete <> writes:
    >
    > >> printf("%s %c\n",*++p,**p);

    > >
    > >The above line is unspecified behavior
    > >depending on which argument is evaluated first.

    >
    > Nope, it's undefined behaviour, because the old value of p is used for
    > other purposes than computing its new value, with no intervening
    > sequence point.


    Thank you.

    --
    pete
     
    pete, Mar 3, 2004
    #6
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,558
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,162
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    907
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,159
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    628
    James Kanze
    Sep 4, 2007
Loading...

Share This Page