input a string in gcc

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

    input a string in gcc

    following is the code to accept a string from user, but i get
    segmentation fault as soon i have finished entering the string i.e. as
    soon as i press the 'enter' key.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
    char str[50];
    printf ("Enter string:\n");
    fgets (str, sizeof str, stdin);
    //further code
    }
    if anybody can help me enter the string, thanx!
  • Walter Roberson

    #2
    Re: input a string in gcc

    In article <e53d3637-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com>,
    yogicoder <zaperaj@gmail. comwrote:
    >following is the code to accept a string from user, but i get
    >segmentation fault as soon i have finished entering the string i.e. as
    >soon as i press the 'enter' key.
    >#include <stdio.h>
    >#include <string.h>
    >#include <stdlib.h>
    >int main (void)
    >{
    > char str[50];
    > printf ("Enter string:\n");
    > fgets (str, sizeof str, stdin);
    That part looks okay to me.
    //further code
    >}
    You are not returning a value from main(), which leads to undefined
    behaviour in C89/C90.

    But I suspect the error is in the "//further code" part.
    --
    "Allegories are in the realm of thoughts, what ruins are in
    the realm of things." -- Walter Benjamin

    Comment

    • Harald van =?UTF-8?b?RMSzaw==?=

      #3
      Re: input a string in gcc

      On Sun, 08 Jun 2008 07:38:48 +0000, Walter Roberson wrote:
      In article
      <e53d3637-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com>,
      yogicoder <zaperaj@gmail. comwrote:
      >>following is the code to accept a string from user, but i get
      >>segmentatio n fault as soon i have finished entering the string i.e. as
      >>soon as i press the 'enter' key.
      >
      >>#include <stdio.h>
      >>#include <string.h>
      >>#include <stdlib.h>
      >
      >>int main (void)
      >>{
      >> char str[50];
      >> printf ("Enter string:\n");
      >> fgets (str, sizeof str, stdin);
      >
      That part looks okay to me.
      >
      > //further code
      >>}
      >
      You are not returning a value from main(),
      If you treat //further code as just a comment, then you already know this
      is not a C90 program. If you treat it as an indicator that there is more
      code, that code may very well return a value from main().
      which leads to undefined
      behaviour in C89/C90.
      No, it doesn't. It leads to an undefined return value, but not undefined
      behaviour, in C90.
      But I suspect the error is in the "//further code" part.
      Agreed.

      Comment

      • Filipe Cabecinhas

        #4
        Re: input a string in gcc

        yogicoder <zaperaj@gmail. comwrites:
        following is the code to accept a string from user, but i get
        segmentation fault as soon i have finished entering the string i.e. as
        soon as i press the 'enter' key.
        The error must be in the // further code part, as others have pointed
        out. Compile your code with the -g flag and use valgrind or gdb to
        see the offending line. After that try to fix it and, if you can't
        (but TRY first) ask for help again.


        --

        - Filipe Cabecinhas

        char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
        puts("Real signature:");
        for (int i = 0; i < sizeof(params)/sizeof(*params) ; ++i)
        printf("%s", params[i]);
        putchar('
        ');

        Comment

        • Richard Tobin

          #5
          Re: input a string in gcc

          In article <e53d3637-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com>,
          yogicoder <zaperaj@gmail. comwrote:
          >following is the code to accept a string from user, but i get
          >segmentation fault as soon i have finished entering the string i.e. as
          >soon as i press the 'enter' key.
          Does this very program get a segmentation fault, or only when you
          put in the "further code"?

          -- Richard
          --
          In the selection of the two characters immediately succeeding the numeral 9,
          consideration shall be given to their replacement by the graphics 10 and 11 to
          facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

          Comment

          • Bartc

            #6
            Re: input a string in gcc


            "yogicoder" <zaperaj@gmail. comwrote in message
            news:e53d3637-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com...
            following is the code to accept a string from user, but i get
            segmentation fault as soon i have finished entering the string i.e. as
            soon as i press the 'enter' key.
            How soon exactly? Enough time for your computer to execute a hundred million
            lines of code following the fgets perhaps?

            If you put in a printf or puts statement just before your '//further code'
            you can establish whether the problem is with the fgets, or with the
            'further code'.

            --
            Bartc


            Comment

            • Richard

              #7
              Re: input a string in gcc

              yogicoder <zaperaj@gmail. comwrites:
              following is the code to accept a string from user, but i get
              segmentation fault as soon i have finished entering the string i.e. as
              soon as i press the 'enter' key.
              >
              Code:
              #include <stdio.h>
              #include <string.h>
              #include <stdlib.h>
              >
              int main (void)
              {
              	char str[50];
              	printf ("Enter string:\n");
              	fgets (str, sizeof str, stdin);
                      //further code
              }
              >
              if anybody can help me enter the string, thanx!
              use a debugger and find where it happens.

              Comment

              • yogicoder

                #8
                Re: input a string in gcc

                On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.co mwrote:
                "yogicoder" <zape...@gmail. comwrote in message
                >
                news:e53d3637-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com...
                >
                following is the code to accept a string from user, but i get
                segmentation fault as soon i have finished entering the string i.e. as
                soon as i press the 'enter' key.
                >
                How soon exactly? Enough time for your computer to execute a hundred million
                lines of code following the fgets perhaps?
                >
                If you put in a printf or puts statement just before your '//further code'
                you can establish whether the problem is with the fgets, or with the
                'further code'.
                >
                --
                Bartc
                Well i didn't consider further code important because i had already
                used a printf("test") statement immediately after fgets & it's not
                getting executed. I get seg fault eror the instant i hit the enter
                key.

                Comment

                • Joe Wright

                  #9
                  Re: input a string in gcc

                  yogicoder wrote:
                  On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.co mwrote:
                  >"yogicoder" <zape...@gmail. comwrote in message
                  >>
                  >news:e53d363 7-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com...
                  >>
                  >>following is the code to accept a string from user, but i get
                  >>segmentatio n fault as soon i have finished entering the string i.e. as
                  >>soon as i press the 'enter' key.
                  >How soon exactly? Enough time for your computer to execute a hundred million
                  >lines of code following the fgets perhaps?
                  >>
                  >If you put in a printf or puts statement just before your '//further code'
                  >you can establish whether the problem is with the fgets, or with the
                  >'further code'.
                  >>
                  >--
                  >Bartc
                  >
                  Well i didn't consider further code important because i had already
                  used a printf("test") statement immediately after fgets & it's not
                  getting executed. I get seg fault eror the instant i hit the enter
                  key.
                  There must be something else going on. The following works fine here.

                  #include <stdio.h>

                  int main(void)
                  {
                  char str[50];
                  printf("Enter string:\n");
                  fgets(str, sizeof str, stdin);
                  fputs(str, stdout);
                  return 0;
                  }


                  --
                  Joe Wright
                  "Everything should be made as simple as possible, but not simpler."
                  --- Albert Einstein ---

                  Comment

                  • Bartc

                    #10
                    Re: input a string in gcc


                    "yogicoder" <zaperaj@gmail. comwrote in message
                    news:c4d253ec-5885-48b8-a6f3-b992f9010699@u6 g2000prc.google groups.com...
                    On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.co mwrote:
                    >"yogicoder" <zape...@gmail. comwrote in message
                    >>
                    >news:e53d363 7-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com...
                    >>
                    following is the code to accept a string from user, but i get
                    segmentation fault as soon i have finished entering the string i.e. as
                    soon as i press the 'enter' key.
                    >>
                    >How soon exactly? Enough time for your computer to execute a hundred
                    >million
                    >lines of code following the fgets perhaps?
                    >>
                    >If you put in a printf or puts statement just before your '//further
                    >code'
                    >you can establish whether the problem is with the fgets, or with the
                    >'further code'.
                    >>
                    >--
                    >Bartc
                    >
                    Well i didn't consider further code important because i had already
                    used a printf("test") statement immediately after fgets & it's not
                    getting executed. I get seg fault eror the instant i hit the enter
                    key.
                    Thanks, that's useful clarification. So in this program (I've added \n to
                    the printf):

                    #include <stdio.h>
                    #include <string.h>
                    #include <stdlib.h>

                    int main (void)
                    {
                    char str[50];
                    printf ("Enter string:\n");
                    fgets (str, sizeof str, stdin);
                    printf("test\n" );
                    }

                    after you hit Enter (for test purposes, don't type anything else), it does
                    not print "test"? I'm asking because on my machine (and I suspect many
                    others) it works as expected.

                    What happens if you comment out the fgets line?

                    --
                    Bartc


                    Comment

                    • Richard

                      #11
                      Re: input a string in gcc

                      "Bartc" <bc@freeuk.comw rites:
                      "yogicoder" <zaperaj@gmail. comwrote in message
                      news:c4d253ec-5885-48b8-a6f3-b992f9010699@u6 g2000prc.google groups.com...
                      >On Jun 8, 4:10 pm, "Bartc" <b...@freeuk.co mwrote:
                      >>"yogicoder" <zape...@gmail. comwrote in message
                      >>>
                      >>news:e53d36 37-81f2-43d3-b5a9-df9db30998a9@f2 4g2000prh.googl egroups.com...
                      >>>
                      >following is the code to accept a string from user, but i get
                      >segmentation fault as soon i have finished entering the string i.e. as
                      >soon as i press the 'enter' key.
                      >>>
                      >>How soon exactly? Enough time for your computer to execute a hundred
                      >>million
                      >>lines of code following the fgets perhaps?
                      >>>
                      >>If you put in a printf or puts statement just before your '//further
                      >>code'
                      >>you can establish whether the problem is with the fgets, or with the
                      >>'further code'.
                      >>>
                      >>--
                      >>Bartc
                      >>
                      >Well i didn't consider further code important because i had already
                      >used a printf("test") statement immediately after fgets & it's not
                      >getting executed. I get seg fault eror the instant i hit the enter
                      >key.
                      >
                      Thanks, that's useful clarification. So in this program (I've added \n to
                      the printf):
                      >
                      #include <stdio.h>
                      #include <string.h>
                      #include <stdlib.h>
                      >
                      int main (void)
                      {
                      char str[50];
                      printf ("Enter string:\n");
                      fgets (str, sizeof str, stdin);
                      printf("test\n" );
                      }
                      >
                      after you hit Enter (for test purposes, don't type anything else), it does
                      not print "test"? I'm asking because on my machine (and I suspect many
                      others) it works as expected.
                      >
                      What happens if you comment out the fgets line?
                      use a debugger. get a backtrace.

                      Comment

                      • Filipe Cabecinhas

                        #12
                        Re: input a string in gcc

                        yogicoder <zaperaj@gmail. comwrites:
                        Well i didn't consider further code important because i had already
                        used a printf("test") statement immediately after fgets & it's not
                        getting executed. I get seg fault eror the instant i hit the enter
                        key.
                        Install valgrind, run your program with: valgrind ./prog
                        then show us the errors.

                        --

                        - Filipe Cabecinhas

                        char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
                        puts("Real signature:");
                        for (int i = 0; i < sizeof(params)/sizeof(*params) ; ++i)
                        printf("%s", params[i]);
                        putchar('\n');

                        Comment

                        • Richard Tobin

                          #13
                          Re: input a string in gcc

                          In article <c4d253ec-5885-48b8-a6f3-b992f9010699@u6 g2000prc.google groups.com>,
                          yogicoder <zaperaj@gmail. comwrote:
                          >Well i didn't consider further code important because i had already
                          >used a printf("test") statement immediately after fgets & it's not
                          >getting executed. I get seg fault eror the instant i hit the enter
                          >key.
                          When you're asking other people to help fix your code, it's not a good
                          idea to rely on what *you* think is important. After all, if you knew
                          what was important you'd be able to find the bug yourself.

                          How do you know the printf("test") statement is not being executed?
                          Do you really mean that "test" isn't being printed? Normally terminal
                          output is line-buffered, so it won't get printed until you output
                          a newline.

                          If you want help, send a complete program that fails, not a cut-down
                          program that you haven't tested.

                          -- Richard
                          --
                          In the selection of the two characters immediately succeeding the numeral 9,
                          consideration shall be given to their replacement by the graphics 10 and 11 to
                          facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                          Comment

                          • pete

                            #14
                            Re: input a string in gcc

                            Richard Tobin wrote:
                            In article <c4d253ec-5885-48b8-a6f3-b992f9010699@u6 g2000prc.google groups.com>,
                            yogicoder <zaperaj@gmail. comwrote:
                            >
                            >Well i didn't consider further code important because i had already
                            >used a printf("test") statement immediately after fgets & it's not
                            >getting executed. I get seg fault eror the instant i hit the enter
                            >key.
                            >
                            When you're asking other people to help fix your code, it's not a good
                            idea to rely on what *you* think is important. After all, if you knew
                            what was important you'd be able to find the bug yourself.
                            Anytime somebody introduces a debugging problem
                            by saying where the problem isn't,
                            that's where I look first.

                            --
                            pete

                            Comment

                            • yogicoder

                              #15
                              Re: input a string in gcc

                              On Jun 8, 7:17 pm, pete <pfil...@mindsp ring.comwrote:
                              Richard Tobin wrote:
                              In article <c4d253ec-5885-48b8-a6f3-b992f9010...@u6 g2000prc.google groups.com>,
                              yogicoder <zape...@gmail. comwrote:
                              >
                              Well i didn't consider further code important because i had already
                              used a printf("test") statement immediately after fgets & it's not
                              getting executed. I get seg fault eror the instant i hit the enter
                              key.
                              >
                              When you're asking other people to help fix your code, it's not a good
                              idea to rely on what *you* think is important. After all, if you knew
                              what was important you'd be able to find the bug yourself.
                              >
                              Anytime somebody introduces a debugging problem
                              by saying where the problem isn't,
                              that's where I look first.
                              >
                              --
                              pete
                              well yes people you all were right. There is some error in the code
                              after fgets. What was happening was that the printf immediately after
                              fgets was also not getting executed because of some error further away
                              in my code. This was my first post so please don't castigate me for
                              this.
                              So here's a detailed explanation:
                              Basically what my code is supposed to do is accept a string from user
                              & reverse the position of the words, for e.g.
                              Input: this is a string
                              Output: string a is this
                              I have to do this in O(n) complexity.
                              Here's my code :
                              [CODE]
                              #include <stdio.h>
                              #include <string.h>
                              #include <stdlib.h>

                              int main (void)
                              {

                              void change (char []);
                              char str[50];
                              printf ("Enter string:\n");
                              fgets (str, sizeof str, stdin);
                              change (str);
                              printf ("\nRequired string is:\n");
                              fputs (str, stdout);
                              return 0;
                              }

                              void change (char str[50])
                              {
                              char *arr[20], *a;
                              int i, j, k;
                              i = j = 0;
                              while (str[j] != '\n') {
                              *arr[i] = str[j];
                              if (str[j] == ' ')
                              ++i;
                              str[++j];
                              arr[i]++;
                              }
                              *arr[i] = '\0';
                              k = j = i;
                              i = 0;
                              while (i < j) {
                              a = arr[i];
                              arr[i] = arr[j];
                              arr[j] = a;
                              ++i;
                              --j;
                              }
                              i = j = 0;
                              while (*arr[i] != '\0') {
                              str[j] = *arr[i];
                              if (*arr[i] == ' ')
                              ++i;
                              str[++j];
                              arr[i]++;
                              }
                              str[j] = *arr[i];
                              return;
                              }

                              There's some error with pointers i know, but i am not able to
                              understand it. You guys have really been helpful so thanks a lot! I've
                              spent a lot of time on this so help would be greatly appreciated.

                              Comment

                              Working...