|  | what 's wrong with this code? |  | |
| | | Siju |  |
| Posted: Sun Aug 31, 2008 4:15 pm Post subject: what 's wrong with this code? |  |
The following code ================================= $ cat string.c #include <string.h>
char * Basename(char *pathname) { char *cp; if ( (cp = strrchr(pathname,'/') ) 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 |  |
| Posted: Sun Aug 31, 2008 4:15 pm Post subject: Re: what 's wrong with this code? |  |
Siju wrote:
| Quote: | The following code ================================= $ cat string.c #include <string.h
char * Basename(char *pathname) { char *cp; if ( (cp = strrchr(pathname,'/') ) 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(pathname,'/')) ) ^-here -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- |
| |
| | | Andrew Poelstra |  |
| Posted: Sun Aug 31, 2008 4:15 pm Post subject: Re: what 's wrong with this code? |  |
On 2008-08-31, Siju <sgeorge.ml@gmail.com> wrote:
| Quote: | 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@wpsoftware.com To email me, use the above email addresss with .com set to .net |
| |
| | | Barry Schwarz |  |
| Posted: Sun Aug 31, 2008 4:15 pm Post subject: Re: what 's wrong with this code? |  |
On Sun, 31 Aug 2008 09:15:25 -0700 (PDT), Siju <sgeorge.ml@gmail.com> wrote:
| Quote: | The following code ================================= $ cat string.c #include <string.h
char * Basename(char *pathname) { char *cp; if ( (cp = strrchr(pathname,'/') ) 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.
-- Remove del for email |
| |
| | | Barry Schwarz |  |
| Posted: Sun Aug 31, 2008 4:15 pm Post subject: Re: what 's wrong with this code? |  |
On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra <apoelstra@supernova.home> wrote:
| Quote: | On 2008-08-31, Siju <sgeorge.ml@gmail.com> wrote:
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.
| Quote: | 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 |
| |
| | | Martin Ambuhl |  |
| Posted: Sun Aug 31, 2008 5:34 pm Post subject: Re: what 's wrong with this code? |  |
Siju wrote:
| Quote: | if ( (cp = strrchr(pathname,'/') ) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
unbalanced parentheses > return cp+1; |
| |
| | | Old Wolf |  |
| Posted: Mon Sep 01, 2008 12:35 am Post subject: Re: what 's wrong with this code? |  |
On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel.com> wrote:
| Quote: | On 2008-08-31, Siju <sgeorge...@gmail.com> wrote:
================================> >> $ 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. |
| |
| | | Keith Thompson |  |
| Posted: Mon Sep 01, 2008 1:00 am Post subject: Re: what 's wrong with this code? |  |
| |  | |
Old Wolf <oldwolf@inspire.net.nz> writes:
| Quote: | On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel.com> wrote: On 2008-08-31, Siju <sgeorge...@gmail.com> wrote: ================================= $ 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_Keith) 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" |
| |
| | | Guest |  |
| Posted: Mon Sep 01, 2008 1:42 am Post subject: Re: what 's wrong with this code? |  |
On Sep 1, 3:35 am, Old Wolf <oldw...@inspire.net.nz> wrote:
| Quote: | On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel.com> wrote:
On 2008-08-31, Siju <sgeorge...@gmail.com> wrote:
================================= $ 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. |
| |
| | | Jensen Somers |  |
| Posted: Mon Sep 01, 2008 6:35 am Post subject: Re: what 's wrong with this code? |  |
| |  | |
Siju wrote:
| Quote: | On Aug 31, 9:52 pm, Barry Schwarz <schwa...@dqel.com> wrote: On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
apoels...@supernova.home> wrote: On 2008-08-31, Siju <sgeorge...@gmail.com> wrote: 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(pathname,'/') ) ) return cp+1; return pathname; } $ ========================================================
I get this error
======================================================== $ cc string.c -o string /usr/lib/crt0.o(.text+0xa4): In function `___start': : undefined reference to `main' collect2: ld returned 1 exit status =========================================================
thanks
--Siju
|
That's because you try to compile this file into a standalone executable but you do not have a main() function.
-- Jensen Somers <http://jsomers.eu> Email: -http:// +jensen@
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|