guess the output

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

    guess the output

    here is a program..
    struct st
    {
    char ch[3];
    int a;
    }st_var={"ABC", 68};

    void main()
    {

    printf("%s",st_ var.ch);

    }

    If there is continuous allocation then the output will be

    ABCD

    now...suppose tht the memory is not allocated continuously..
    the first 3 bytes are allocated at 1 place and next 2 bytes are
    allocated at other place
    wht will be the output....???
  • Richard Bos

    #2
    Re: guess the output

    jt <karthiks.840@g mail.comwrote:
    wht will be the output....???
    You'd be better off not asking that question, and learning to write
    programs that aren't broken instead. Any answer you do find will be
    highly undependable, and likely to lead to crashes or worse further down
    the line.

    Richard

    Comment

    • Joachim Schmitz

      #3
      Re: guess the output

      santosh wrote:
      jt wrote:
      >
      >here is a program..
      >struct st
      >{
      > char ch[3];
      > int a;
      >}st_var={"ABC" ,68};
      >
      Undefined behaviour. You are writing past the bounds of an object, in
      this case 'ch'.
      >
      >void main()
      >
      int main(void)
      >
      >{
      >>
      > printf("%s",st_ var.ch);
      >
      return 0;
      >
      >>
      >}
      >>
      >If there is continuous allocation then the output will be
      >>
      >ABCD
      >
      Only for the ASCII encoding.
      And only for little endian machines...

      Bye, Jojo


      Comment

      • Martin Ambuhl

        #4
        Re: guess the output

        santosh wrote:
        jt wrote:
        >
        >here is a program..
        >struct st
        >{
        > char ch[3];
        > int a;
        >}st_var={"ABC" ,68};
        >
        Undefined behaviour. You are writing past the bounds of an object, in
        this case 'ch'.
        Not true. The initializer "ABC" is perfectly fine for char ch[3]. It
        just won't give you a string.


        Comment

        • Joachim Schmitz

          #5
          Re: guess the output

          Joachim Schmitz wrote:
          santosh wrote:
          >jt wrote:
          >>
          >>here is a program..
          >>struct st
          >>{
          >> char ch[3];
          >> int a;
          >>}st_var={"ABC ",68};
          >>
          >Undefined behaviour. You are writing past the bounds of an object, in
          >this case 'ch'.
          >>
          >>void main()
          >>
          >int main(void)
          >>
          >>{
          >>>
          >> printf("%s",st_ var.ch);
          >>
          >return 0;
          >>
          >>>
          >>}
          >>>
          >>If there is continuous allocation then the output will be
          >>>
          >>ABCD
          >>
          >Only for the ASCII encoding.
          And only for little endian machines...
          And sizeof(int) == 2

          Bye, Jojo


          Comment

          • Martin

            #6
            Re: guess the output

            On Mon, 03 Mar 2008 14:48:00 -0000, Kenneth Brody <kenbrody@spamc op.net>
            wrote:
            >
            Are you sure? I thought this was perfectly valid:
            >
            char chr[3] = "ABC";
            >
            In this case, it's simply shorthand for:
            >
            char chr[3] = { 'A', 'B', 'C' };
            >
            [...]
            According to K&R2 it is correct:

            If the array has unknown size, the number of
            characters in the string, including the
            terminating null character, determines its
            size; if its size is fixed, the number of
            characters in the string, not counting the
            terminating null character, must not exceed
            the size of the array. -- Section A8.7

            Presumably this is an accurate interpretation of the standard that was
            later ratified and referred to informally as C89.

            --
            Martin

            Comment

            • Richard Heathfield

              #7
              Re: guess the output

              Martin said:

              <snip>
              If the array has unknown size, the number of
              characters in the string, including the
              terminating null character, determines its
              size; if its size is fixed, the number of
              characters in the string, not counting the
              terminating null character, must not exceed
              the size of the array. -- Section A8.7
              >
              Presumably this is an accurate interpretation of the standard that was
              later ratified and referred to informally as C89.
              Yes, and this oddity survives in C99, in 6.7.8(14):

              "An array of character type may be initialized by a character string
              literal, optionally enclosed in braces. Successive characters of the
              character string literal (including the terminating null character if
              there is room or if the array is of unknown size) initialize the
              elements of the array."

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

              Comment

              Working...