473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Aug 31 '08 #1
13 1409
Siju wrote:
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 ---
Aug 31 '08 #2
On 2008-08-31, Siju <sg********@gmail.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 ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
Aug 31 '08 #3
On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra
<ap*******@supernova.homewrote:
>On 2008-08-31, Siju <sg********@gmail.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
Aug 31 '08 #4
On Sun, 31 Aug 2008 09:15:25 -0700 (PDT), Siju <sg********@gmail.com>
wrote:
>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.
return pathname;
}
--
Remove del for email
Aug 31 '08 #5
Siju wrote:
if ( (cp = strrchr(pathname,'/') )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
unbalanced parentheses
return cp+1;
Aug 31 '08 #6
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
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"
================================
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.
Sep 1 '08 #7
On Sep 1, 3:35 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Sep 1, 4:52 am, Barry Schwarz <schwa...@dqel.comwrote:
>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"
>>================================
>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.
Sep 1 '08 #8
Old Wolf <ol*****@inspire.net.nzwrites:
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
>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"
================================
>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) ks***@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"
Sep 1 '08 #9
On Aug 31, 9:52 pm, Barry Schwarz <schwa...@dqel.comwrote:
On Sun, 31 Aug 2008 16:28:49 GMT, Andrew Poelstra

<apoels...@supernova.homewrote:
On 2008-08-31, Siju <sgeorge...@gmail.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(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
Sep 1 '08 #10
Hi

On Sun, 31 Aug 2008 09:15:25 -0700, Siju wrote:
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
Sep 1 '08 #11
On Mon, 01 Sep 2008 11:25:59 +0000, viza wrote:
...This is the case with the standard basename(1) ...
basename(3)
Sep 1 '08 #12
On Sep 1, 3:00*pm, Keith Thompson <ks...@mib.orgwrote:
Old Wolf <oldw...@inspire.net.nzwrites:
On Sep 1, 4:52*am, Barry Schwarz <schwa...@dqel.comwrote:
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"
================================
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'.

Sep 1 '08 #13
rio

"Siju" <sg********@gmail.comha scritto nel messaggio
news:7f**********************************@s1g2000p ra.googlegroups.com...
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;}
$
=================================

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


Sep 2 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.