entering more characters

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

    #1

    entering more characters

    Hi all,

    in the following code, i enter more characters into a character array.
    array size declared as 80. i enter more characters then i get
    segmentation fault. if anybody knows give me reply.
    thanks in advance.

    Regards
    kk


    main()
    {
    char s[80];
    scanf("%[^\n]s",s);
    printf("%s",s);
    }

  • Suman

    #2
    Re: entering more characters



    kk wrote:[color=blue]
    > Hi all,
    >
    > in the following code, i enter more characters into a character array.
    > array size declared as 80. i enter more characters then i get
    > segmentation fault. if anybody knows give me reply.[/color]
    Are you entering more than 80 characters?
    Writing past array bounds is your problem.[color=blue]
    > thanks in advance.
    >
    > Regards
    > kk
    >
    >
    > main()
    > {
    > char s[80];
    > scanf("%[^\n]s",s);
    > printf("%s",s);
    > }[/color]

    Comment

    • Barry

      #3
      Re: entering more characters

      Why don't u do a bounce check? never get past an array limit


      kk wrote:[color=blue]
      > Hi all,
      >
      > in the following code, i enter more characters into a character array.
      > array size declared as 80. i enter more characters then i get
      > segmentation fault. if anybody knows give me reply.
      > thanks in advance.
      >
      > Regards
      > kk
      >
      >
      > main()
      > {
      > char s[80];
      > scanf("%[^\n]s",s);
      > printf("%s",s);
      > }[/color]

      Comment

      • Suman

        #4
        Re: entering more characters



        Suman wrote:[color=blue]
        > kk wrote:[color=green]
        > > Hi all,
        > >
        > > in the following code, i enter more characters into a character array.
        > > array size declared as 80. i enter more characters then i get
        > > segmentation fault. if anybody knows give me reply.[/color]
        > Are you entering more than 80 characters?[/color]
        Ah! Off by one! You can't have more than 79 characters in your
        `s' thingy.[color=blue]
        > Writing past array bounds is your problem.[color=green]
        > > thanks in advance.
        > >
        > > Regards
        > > kk
        > >
        > >
        > > main()
        > > {
        > > char s[80];
        > > scanf("%[^\n]s",s);
        > > printf("%s",s);
        > > }[/color][/color]
        The following might be of interest to you:

        thread/4612b24419b17c7 8/27bbc52f5be7f3b e?
        q=pete+%2B+scan f&rnum=6&hl=en# 27bbc52f5be7f3b e

        Comment

        • Kenny McCormack

          #5
          Encouraging illegal behavior is O/T in clc (Was: Re: entering more characters)

          In article <1122466531.889 573.144440@o13g 2000cwo.googleg roups.com>,
          Barry <prbharath@gmai l.com> wrote:[color=blue]
          >Why don't u do a bounce check? never get past an array limit[/color]

          I think bouncing checks is illegal.

          Comment

          • Lew Pitcher

            #6
            Re: entering more characters

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

            kk wrote:[color=blue]
            > Hi all,
            >
            > in the following code, i enter more characters into a character array.
            > array size declared as 80. i enter more characters then i get
            > segmentation fault. if anybody knows give me reply.
            > thanks in advance.[/color]

            A truism that my mother taught me applies here...

            "You cant stuff a size 10 foot into size 7 shoes. Either the shoes split
            or your foot does."

            In this case, you can't stuff more than 80 chars into an 80 char array.
            If you do, you write over something (what, we cannot tell you) outside
            of the array, and cause your program to fall over.

            In other words (to quote Mel Brooks) "Don't do that."


            - --

            Lew Pitcher, IT Specialist, Enterprise Data Systems
            Enterprise Technology Solutions, TD Bank Financial Group

            (Opinions expressed here are my own, not my employer's)
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.2.4 (MingW32)

            iD8DBQFC54OMagV FX4UWr64RAjdCAK DU7DZ4Ot0WxHF5v RZRCP2iG8VqBACf d7HD
            nRoS+pj0njgGDI/r4rgynpU=
            =xJza
            -----END PGP SIGNATURE-----

            Comment

            • Walter Roberson

              #7
              Re: entering more characters

              In article <1122465724.574 698.280280@g44g 2000cwa.googleg roups.com>,
              kk <kirancareers@g mail.com> wrote:[color=blue]
              >in the following code, i enter more characters into a character array.
              >array size declared as 80. i enter more characters then i get
              >segmentation fault. if anybody knows give me reply.
              >thanks in advance.[/color]
              [color=blue]
              >main()
              >{
              > char s[80];
              > scanf("%[^\n]s",s);
              > printf("%s",s);
              >}[/color]


              I -suspect- that the answer you are looking for is:

              #include <stdio.h>
              int main(void) {
              char s[80];
              scanf("%79[^\n]", s);
              printf("%s",s);
              }


              Note that if you use this and the input stream has more than 79
              characters before the newline, then the remaining characters will
              remain in the input buffer. Also, watch out because even if a newline
              was supplied, it is going to remain in the input buffer.

              Also note that the format specifier ends at the ] and that the
              s that you had after that would be treated as a literal character
              to be matched -- the format specifier is not %[something]s
              just %[something]
              --
              Ceci, ce n'est pas une idée.

              Comment

              • Jack Klein

                #8
                Re: entering more characters

                On 27 Jul 2005 05:15:31 -0700, "Barry" <prbharath@gmai l.com> wrote in
                comp.lang.c:

                1. Stop top posting.
                [color=blue]
                > Why don't u do a bounce check? never get past an array limit[/color]

                2. Don't post nonsense.

                -- 'u' doesn't post here anymore, we kicked him out.

                -- C doesn't have anything called a "bounce check".

                -- There is no way to stop scanf "%s" without a length value from
                overflowing a small buffer. On some implementations , there is no way
                to prevent it from overflowing any size buffer.
                [color=blue]
                > kk wrote:[color=green]
                > > Hi all,
                > >
                > > in the following code, i enter more characters into a character array.
                > > array size declared as 80. i enter more characters then i get
                > > segmentation fault. if anybody knows give me reply.
                > > thanks in advance.
                > >
                > > Regards
                > > kk
                > >
                > >
                > > main()
                > > {
                > > char s[80];
                > > scanf("%[^\n]s",s);
                > > printf("%s",s);
                > > }[/color][/color]

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

                Comment

                Working...