what 's wrong with this code?

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

    what 's wrong with this code?

    The following code
    =============== =============== ===
    $ cat
    string.c
    #include <string.h>

    char *
    Basename(char *pathname) {
    char *cp;
    if ( (cp = strrchr(pathnam e,'/') )
    return cp+1;
    return pathname;
    }
    $
    =============== =============== ===

    gives the following error.

    =============== =============== ===
    $ cc string.c -o
    string
    string.c: In function `Basename':
    string.c:7: error: syntax error before "return"
    =============== =============== ==

    Could some one help me find the error?
    I am new to C lang

    Thanks

    --Siju



    gives
  • Joe Wright

    #2
    Re: what 's wrong with this code?

    Siju wrote:
    The following code
    =============== =============== ===
    $ cat
    string.c
    #include <string.h>
    >
    char *
    Basename(char *pathname) {
    char *cp;
    if ( (cp = strrchr(pathnam e,'/') )
    return cp+1;
    return pathname;
    }
    $
    =============== =============== ===
    >
    gives the following error.
    >
    =============== =============== ===
    $ cc string.c -o
    string
    string.c: In function `Basename':
    string.c:7: error: syntax error before "return"
    =============== =============== ==
    >
    Could some one help me find the error?
    I am new to C lang
    >
    Thanks
    >
    You missed a right paren. Should be..
    if ( (cp = strrchr(pathnam e,'/')) )
    ^-here
    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---

    Comment

    • Andrew Poelstra

      #3
      Re: what 's wrong with this code?

      On 2008-08-31, Siju <sgeorge.ml@gma il.comwrote:
      >
      gives the following error.
      >
      >============== =============== ====
      $ cc string.c -o
      string
      string.c: In function `Basename':
      string.c:7: error: syntax error before "return"
      >============== =============== ===
      >
      You are missing a right parentheses and the entire main()
      function. Both are required for the program to compile.
      Surely you could figure that one out without resorting to
      asking on a newsgroup?

      I have snipped the code.

      --
      Andrew Poelstra apoelstra@wpsof tware.com
      To email me, use the above email addresss with .com set to .net

      Comment

      • Barry Schwarz

        #4
        Re: what 's wrong with this code?

        On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
        <apoelstra@supe rnova.homewrote :
        >On 2008-08-31, Siju <sgeorge.ml@gma il.comwrote:
        >>
        >gives the following error.
        >>
        >>============= =============== =====
        >$ cc string.c -o
        >string
        >string.c: In function `Basename':
        >string.c:7: error: syntax error before "return"
        >>============= =============== ====
        >>
        >
        >You are missing a right parentheses and the entire main()
        >function. Both are required for the program to compile.
        There is no need for main to be present in this source file. It is
        obviously needed to execute (on a hosted system) but not needed to
        compile.
        >Surely you could figure that one out without resorting to
        >asking on a newsgroup?
        Not since there is no such requirement and even if there were it is
        completely unrelated to the question at hand.

        --
        Remove del for email

        Comment

        • Barry Schwarz

          #5
          Re: what 's wrong with this code?

          On Sun, 31 Aug 2008 09:15:25 -0700 (PDT), Siju <sgeorge.ml@gma il.com>
          wrote:
          >The following code
          >============== =============== ====
          >$ cat
          >string.c
          >#include <string.h>
          >
          >char *
          >Basename(cha r *pathname) {
          char *cp;
          if ( (cp = strrchr(pathnam e,'/') )
          return cp+1;
          Indenting the range of if (as well as for, while, and do-while) will
          make your code a lot easier to read and save you a lot of time if you
          have to come back to it months later.
          return pathname;
          >}
          --
          Remove del for email

          Comment

          • Martin Ambuhl

            #6
            Re: what 's wrong with this code?

            Siju wrote:
            if ( (cp = strrchr(pathnam e,'/') )
            ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
            unbalanced parentheses
            return cp+1;

            Comment

            • Old Wolf

              #7
              Re: what 's wrong with this code?

              On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel. comwrote:
              On 2008-08-31, Siju <sgeorge...@gma il.comwrote:
              >
              >============== =============== ====
              $ cc string.c -o
              string
              string.c: In function `Basename':
              string.c:7: error: syntax error before "return"
              >============== =============== ===
              >
              You are missing a right parentheses and the entire main()
              function. Both are required for the program to compile.
              >
              There is no need for main to be present in this source file.  It is
              obviously needed to execute (on a hosted system) but not needed to
              compile.
              The compilation command given by the OP requests
              the generation of an executable from the single
              source file, string.c . So main() is needed for
              compilation to succeed.

              Comment

              • vippstar@gmail.com

                #8
                Re: what 's wrong with this code?

                On Sep 1, 3:35 am, Old Wolf <oldw...@inspir e.net.nzwrote:
                On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel. comwrote:
                >
                >On 2008-08-31, Siju <sgeorge...@gma il.comwrote:
                >
                >>============= =============== =====
                >$ cc string.c -o
                >string
                >string.c: In function `Basename':
                >string.c:7: error: syntax error before "return"
                >>============= =============== ====
                >
                >You are missing a right parentheses and the entire main()
                >function. Both are required for the program to compile.
                >
                There is no need for main to be present in this source file. It is
                obviously needed to execute (on a hosted system) but not needed to
                compile.
                >
                The compilation command given by the OP requests
                the generation of an executable from the single
                source file, string.c . So main() is needed for
                compilation to succeed.

                How do you know? cc string.c -o string could mean anything.

                Comment

                • Keith Thompson

                  #9
                  Re: what 's wrong with this code?

                  Old Wolf <oldwolf@inspir e.net.nzwrites:
                  On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel. comwrote:
                  >On 2008-08-31, Siju <sgeorge...@gma il.comwrote:
                  >>============= =============== =====
                  >$ cc string.c -o
                  >string
                  >string.c: In function `Basename':
                  >string.c:7: error: syntax error before "return"
                  >>============= =============== ====
                  >>
                  >You are missing a right parentheses and the entire main()
                  >function. Both are required for the program to compile.
                  >>
                  >There is no need for main to be present in this source file.  It is
                  >obviously needed to execute (on a hosted system) but not needed to
                  >compile.
                  >
                  The compilation command given by the OP requests
                  the generation of an executable from the single
                  source file, string.c . So main() is needed for
                  compilation to succeed.
                  No, it's needed for *linking* to succeed. The command, assuming "cc"
                  works the way it does on most Unix-like systems, specifies both
                  compilation and linking.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Siju

                    #10
                    Re: what 's wrong with this code?

                    On Aug 31, 9:52 pm, Barry Schwarz <schwa...@dqel. comwrote:
                    On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
                    >
                    <apoels...@supe rnova.homewrote :
                    On 2008-08-31, Siju <sgeorge...@gma il.comwrote:
                    >
                    gives the following error.
                    >
                    >============== =============== ====
                    $ cc string.c -o
                    string
                    string.c: In function `Basename':
                    string.c:7: error: syntax error before "return"
                    >============== =============== ===
                    >
                    You are missing a right parentheses and the entire main()
                    function. Both are required for the program to compile.
                    >
                    Thank you so much for the responses.

                    But when I added the parentheses

                    =============== =============== =============== ===========
                    $ cat string.c
                    #include <string.h>

                    char *
                    Basename(char *pathname) {
                    char *cp;
                    if ( (cp = strrchr(pathnam e,'/') ) )
                    return cp+1;
                    return pathname;
                    }
                    $
                    =============== =============== =============== ===========

                    I get this error

                    =============== =============== =============== ===========
                    $ cc string.c -o
                    string
                    /usr/lib/crt0.o(.text+0x a4): In function `___start':
                    : undefined reference to `main'
                    collect2: ld returned 1 exit status
                    =============== =============== =============== ============

                    thanks

                    --Siju

                    Comment

                    • viza

                      #11
                      Re: what 's wrong with this code?

                      Hi

                      On Sun, 31 Aug 2008 09:15:25 -0700, Siju wrote:
                      char *
                      Basename(char *pathname) {
                      char *cp;
                      if ( (cp = strrchr(pathnam e,'/') )
                      return cp+1;
                      return pathname;
                      }
                      apart from the missing paren, your function mis-handles handle the
                      following values of pathname:

                      NULL should return "." your version probably crashes
                      "" should return "." your version returns ""
                      "foo/" should return "foo" your version returns ""
                      "/" should return "/" your version returns ""

                      The last two are very big bugs indeed!

                      There is an efficient public domain C implementation of this function and
                      several related ones at:




                      NB: these versions return (char*) cast to (const char*), to give you the
                      hint that it isn't always ok to write to the returned string. This is
                      the case with the standard basename(1) too, but the type doesn't indicate
                      it.

                      HTH
                      viza

                      Comment

                      • viza

                        #12
                        Re: what 's wrong with this code?

                        On Mon, 01 Sep 2008 11:25:59 +0000, viza wrote:
                        ...This is the case with the standard basename(1) ...
                        basename(3)

                        Comment

                        • Old Wolf

                          #13
                          Re: what 's wrong with this code?

                          On Sep 1, 3:00 pm, Keith Thompson <ks...@mib.orgw rote:
                          Old Wolf <oldw...@inspir e.net.nzwrites:
                          On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel. comwrote:
                          On 2008-08-31, Siju <sgeorge...@gma il.comwrote:
                          >============== =============== ====
                          $ cc string.c -o
                          string
                          string.c: In function `Basename':
                          string.c:7: error: syntax error before "return"
                          >============== =============== ===
                          >
                          You are missing a right parentheses and the entire main()
                          function. Both are required for the program to compile.
                          >
                          There is no need for main to be present in this source file.  It is
                          obviously needed to execute (on a hosted system) but not needed to
                          compile.
                          >
                          The compilation command given by the OP requests
                          the generation of an executable from the single
                          source file, string.c . So main() is needed for
                          compilation to succeed.
                          >
                          No, it's needed for *linking* to succeed.  The command, assuming "cc"
                          works the way it does on most Unix-like systems, specifies both
                          compilation and linking.
                          I was using 'compile' in the very common usage
                          of 'generate an executable'.

                          Comment

                          • rio

                            #14
                            Re: what 's wrong with this code?


                            "Siju" <sgeorge.ml@gma il.comha scritto nel messaggio
                            news:7f7516d4-c184-49c6-ac98-a875e9ed80e4@s1 g2000pra.google groups.com...
                            The following code
                            =============== =============== ===
                            $ cat
                            string.c
                            #include <string.h>
                            >
                            char *
                            Basename(char *pathname) {
                            char *cp;
                            if ( (cp = strrchr(pathnam e,'/') )
                            return cp+1;
                            return pathname;
                            }
                            char* Basename(char *pathname) {
                            char *cp;
                            if(pathname)
                            if ( (cp=strrchr(pat hname,'/'))
                            || (cp=strrchr(pat hname,'\\'))
                            ) return cp+1;
                            return pathname;}
                            $
                            =============== =============== ===
                            >
                            gives the following error.
                            >
                            =============== =============== ===
                            $ cc string.c -o
                            string
                            string.c: In function `Basename':
                            string.c:7: error: syntax error before "return"
                            =============== =============== ==
                            >
                            Could some one help me find the error?
                            I am new to C lang
                            >
                            Thanks
                            >
                            --Siju
                            >
                            >
                            >
                            gives


                            Comment

                            Working...