Connecting Tech Pros Worldwide Help | Site Map

what 's wrong with this code?

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 31st, 2008, 04:25 PM
Siju
Guest
 
Posts: n/a
Default 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

  #2  
Old August 31st, 2008, 04:35 PM
Joe Wright
Guest
 
Posts: n/a
Default 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 ---
  #3  
Old August 31st, 2008, 04:35 PM
Andrew Poelstra
Guest
 
Posts: n/a
Default 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
  #4  
Old August 31st, 2008, 04:55 PM
Barry Schwarz
Guest
 
Posts: n/a
Default 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
  #5  
Old August 31st, 2008, 04:55 PM
Barry Schwarz
Guest
 
Posts: n/a
Default 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.
Quote:
return pathname;
>}
--
Remove del for email
  #6  
Old August 31st, 2008, 07:35 PM
Martin Ambuhl
Guest
 
Posts: n/a
Default Re: what 's wrong with this code?

Siju wrote:
Quote:
if ( (cp = strrchr(pathname,'/') )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
unbalanced parentheses
Quote:
return cp+1;
  #7  
Old September 1st, 2008, 12:45 AM
Old Wolf
Guest
 
Posts: n/a
Default 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.
  #8  
Old September 1st, 2008, 01:45 AM
vippstar@gmail.com
Guest
 
Posts: n/a
Default 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.
  #9  
Old September 1st, 2008, 03:05 AM
Keith Thompson
Guest
 
Posts: n/a
Default 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"
  #10  
Old September 1st, 2008, 07:45 AM
Siju
Guest
 
Posts: n/a
Default 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
  #11  
Old September 1st, 2008, 11:35 AM
viza
Guest
 
Posts: n/a
Default 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
  #12  
Old September 1st, 2008, 11:35 AM
viza
Guest
 
Posts: n/a
Default 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)
  #13  
Old September 1st, 2008, 01:15 PM
Old Wolf
Guest
 
Posts: n/a
Default 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'.

  #14  
Old September 2nd, 2008, 04:25 PM
rio
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,989 network members.