about array declare with parenthesis mark

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anson

    #1

    about array declare with parenthesis mark

    #include <stdio.h>

    int
    main(void)

    {
    int s[3] = { 1, (3,2), (6, 5, 4)};

    printf ("%d %d %d", s[0], s[1], s[2]);

    }
    ~
    ~
    >a.out
    1 2 4

    this means the last one within the parenthesis mark will works ,
    what about others ?
    what happened and why .
    I want to know ...
    it's a part of language or depend on implement ?

  • Richard Heathfield

    #2
    Re: about array declare with parenthesis mark

    anson said:
    #include <stdio.h>
    >
    int
    main(void)
    >
    {
    int s[3] = { 1, (3,2), (6, 5, 4)};
    >
    printf ("%d %d %d", s[0], s[1], s[2]);
    >
    }
    ~
    ~
    >>a.out
    1 2 4
    >
    this means the last one within the parenthesis mark will works ,
    what about others ?
    what happened and why .
    I want to know ...
    it's a part of language or depend on implement ?

    See page 62 of Kernighan and Ritchie's "The C Programming Language", 2nd
    edition.


    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Walter Roberson

      #3
      Re: about array declare with parenthesis mark

      In article <1184309282.886 989.192550@o11g 2000prd.googleg roups.com>,
      anson <kzjeef@gmail.c omwrote:
      >#include <stdio.h>
      >int
      >main(void)
      >{
      >int s[3] = { 1, (3,2), (6, 5, 4)};
      >printf ("%d %d %d", s[0], s[1], s[2]);
      >}
      >>a.out
      >1 2 4
      >this means the last one within the parenthesis mark will works ,
      >what about others ?
      >what happened and why .
      You used the "comma" operator, which evaluates each expression
      in turn discarding each value except the last.
      --
      Prototypes are supertypes of their clones. -- maplesoft

      Comment

      • Jack Klein

        #4
        Re: about array declare with parenthesis mark

        On Fri, 13 Jul 2007 06:48:02 -0000, anson <kzjeef@gmail.c omwrote in
        comp.lang.c:
        #include <stdio.h>
        >
        int
        main(void)
        >
        {
        int s[3] = { 1, (3,2), (6, 5, 4)};
        >
        printf ("%d %d %d", s[0], s[1], s[2]);
        >
        }
        ~
        ~
        a.out
        1 2 4
        >
        this means the last one within the parenthesis mark will works ,
        what about others ?
        what happened and why .
        I want to know ...
        it's a part of language or depend on implement ?
        Actually this code is not valid C under versions of the C standard
        earlier than 1999. It is valid under C99 and later, but it would not
        be if the array 's' was defined with static storage duration.

        All objects with static storage duration under all versions of the C
        standard, and all aggregate objects prior to C99, must be initialized
        with constant expressions. And the comma operator is not allowed in
        constant expressions.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        Working...