Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strchr declaration

Reply
Thread Tools

strchr declaration

 
 
ts
Guest
Posts: n/a
 
      03-14-2007
Hello all,

Can somebody explain why strchr is declared the way it is? Here is
the declaration:

char *strchr(const char *s, int c);

Mainly I do not understand why the second parameter has the 'int'
type. From my knowledge it is not even portable. On a machine where
'char' has the same size as an 'int' and where 'char' has the same
behaviour as an 'unsigned char' the following piece of code could not
work as expected:

{
char c;
char *p;

/* some statements here initializing c */

p = strchr(my_string, c);
}

I am not a portability expert, so probably I am wrong. But having the
second parameter of 'char' type is less confusing and does not change
the behaviour at all.

thx
cristi
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      03-14-2007
ts wrote:

> Can somebody explain why strchr is declared the way it is? Here is
> the declaration:
>
> char *strchr(const char *s, int c);
>
> Mainly I do not understand why the second parameter has the 'int'
> type.


Historical reasons, I believe. Remember that `strchr` existed before
function prototypes did, and that a character literal such as 'X'
has type /int/ not type /char/. Suppose (as would be the case for
a /lot/ of code move from pre-Standard to post_Standard) that
`strchr` is declared without a prototype. Then the default argument
promotions apply, so `char`s get promoted to `int`s, so if the
/definition/ of `strchr` has second argument `char` the effects are
undefined. This is a Bad Thing.

> From my knowledge it is not even portable. On a machine where
> 'char' has the same size as an 'int'


.... various problems arise in any case, if I recall correctly. An
easy fix is to make such an implementation freestanding, in which
case they don't have to provide the C standard library [1]!

> I am not a portability expert, so probably I am wrong. But having the
> second parameter of 'char' type is less confusing and does not change
> the behaviour at all.


It would be, but for history; and it does, even if only by a little.

[1] Perhaps this is "fixed" as in "You're well and truly fixed.".

--
Chris "electric hedgehog" Dollin
"What I don't understand is this ..." Trevor Chaplin, /The Beiderbeck Affair/

 
Reply With Quote
 
 
 
 
Florian Weingarten
Guest
Posts: n/a
 
      03-14-2007
ts <(E-Mail Removed)> wrote:
> Can somebody explain why strchr is declared the way it is? Here is
> the declaration:
>
> char *strchr(const char *s, int c);


This has been asked here before. Have a look at:

Message-ID: <o2LIa.12908$KF1.274554@amstwist00>
(http://groups.google.de/group/comp.l...658a994d3c28f/)


HTH
Flo
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strchr earthling C++ 2 11-16-2008 05:21 PM
strchr & finding multiple occurances of a char David Warner C Programming 4 04-26-2005 03:45 PM
a shorter strchr Servé Lau C Programming 9 02-19-2004 02:56 AM
C++ strings and strchr() ataru@nospam.cyberspace.org C++ 32 08-22-2003 05:33 PM
Re: Question about strchr() / strrchr() Martin Dickopp C Programming 4 08-16-2003 07:42 PM



Advertisments