
August 31st, 2008, 05:25 PM
|
|
|
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
|

August 31st, 2008, 05:35 PM
|
|
|
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 ---
|

August 31st, 2008, 05:35 PM
|
|
|
Re: what 's wrong with this code?
On 2008-08-31, Siju <sgeorge.ml@gmail.comwrote:
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
|

August 31st, 2008, 05:55 PM
|
|
|
Re: what 's wrong with this code?
On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
<apoelstra@supernova.homewrote:
Quote:
>On 2008-08-31, Siju <sgeorge.ml@gmail.comwrote:
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.
|
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
|

August 31st, 2008, 05:55 PM
|
|
|
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
|

August 31st, 2008, 08:35 PM
|
|
|
Re: what 's wrong with this code?
Siju wrote:
Quote:
|
if ( (cp = strrchr(pathname,'/') )
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
unbalanced parentheses
|

September 1st, 2008, 01:45 AM
|
|
|
Re: what 's wrong with this code?
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
Quote:
Quote:
|
On 2008-08-31, Siju <sgeorge...@gmail.comwrote:
|
>
Quote:
Quote:
>=================================
$ cc string.c -o
string
string.c: In function `Basename':
string.c:7: error: syntax error before "return"
>================================
|
|
>
Quote:
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.
|

September 1st, 2008, 02:45 AM
|
|
|
Re: what 's wrong with this code?
On Sep 1, 3:35 am, Old Wolf <oldw...@inspire.net.nzwrote:
Quote:
On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel.comwrote:
>
Quote:
Quote:
|
>On 2008-08-31, Siju <sgeorge...@gmail.comwrote:
|
|
>
Quote:
Quote:
>>=================================
>$ cc string.c -o
>string
>string.c: In function `Basename':
>string.c:7: error: syntax error before "return"
>>================================
|
|
>
Quote:
Quote:
>You are missing a right parentheses and the entire main()
>function. Both are required for the program to compile.
|
|
>
Quote:
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.
|

September 1st, 2008, 04:05 AM
|
|
|
Re: what 's wrong with this code?
Old Wolf <oldwolf@inspire.net.nzwrites:
Quote:
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
Quote:
Quote:
>On 2008-08-31, Siju <sgeorge...@gmail.comwrote:
>>=================================
>$ cc string.c -o
>string
>string.c: In function `Basename':
>string.c:7: error: syntax error before "return"
>>================================
|
>>
Quote:
>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"
|

September 1st, 2008, 08:45 AM
|
|
|
Re: what 's wrong with this code?
On Aug 31, 9:52 pm, Barry Schwarz <schwa...@dqel.comwrote:
Quote:
On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
>
<apoels...@supernova.homewrote:
Quote:
|
On 2008-08-31, Siju <sgeorge...@gmail.comwrote:
|
>
Quote:
Quote:
|
gives the following error.
|
|
>
Quote:
Quote:
>=================================
$ cc string.c -o
string
string.c: In function `Basename':
string.c:7: error: syntax error before "return"
>================================
|
|
>
Quote:
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
|

September 1st, 2008, 12:35 PM
|
|
|
Re: what 's wrong with this code?
Hi
On Sun, 31 Aug 2008 09:15:25 -0700, Siju wrote:
Quote:
char *
Basename(char *pathname) {
char *cp;
if ( (cp = strrchr(pathname,'/') )
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:
http://www.purposeful.co.uk/software...e/basename_r.h
http://www.purposeful.co.uk/software...e/basename_r.c
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
|

September 1st, 2008, 12:35 PM
|
|
|
Re: what 's wrong with this code?
On Mon, 01 Sep 2008 11:25:59 +0000, viza wrote:
Quote:
|
...This is the case with the standard basename(1) ...
|
basename(3)
|

September 1st, 2008, 02:15 PM
|
|
|
Re: what 's wrong with this code?
On Sep 1, 3:00*pm, Keith Thompson <ks...@mib.orgwrote:
Quote:
Old Wolf <oldw...@inspire.net.nzwrites:
Quote:
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
Quote:
On 2008-08-31, Siju <sgeorge...@gmail.comwrote:
>=================================
$ cc string.c -o
string
string.c: In function `Basename':
string.c:7: error: syntax error before "return"
>================================
|
|
>
Quote:
Quote:
You are missing a right parentheses and the entire main()
function. Both are required for the program to compile.
|
|
>
Quote:
Quote:
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:
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'.
|

September 2nd, 2008, 05:25 PM
|
|
|
Re: what 's wrong with this code?
"Siju" <sgeorge.ml@gmail.comha scritto nel messaggio
news:7f7516d4-c184-49c6-ac98-a875e9ed80e4@s1g2000pra.googlegroups.com...
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;
}
|
char* Basename(char *pathname) {
char *cp;
if(pathname)
if ( (cp=strrchr(pathname,'/'))
|| (cp=strrchr(pathname,'\\'))
) return cp+1;
return pathname;}
Quote:
$
=================================
>
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
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|