OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] Refreshed genx.h, plus some plans

[ Lists Home | Date Index | Thread Index ]

On Sun, 25 Jan 2004 10:01:14 +0400 (AMT), David Tolpin <dvd@davidashen.net> wrote:

>the problem is that it is not so. Take an ANSI C compiler, turn all warning
>on and try it.

Gee, you'd better tell Microsoft!  Their entire C Library for Visual C
uses const throughout.  <bg>  A few simple examples:

size_t strlen( const char *string );
int strcmp( const char *string1, const char *string2 );
size_t strspn( const char *string1, const char *string2 );
char *strrchr( const char *string, int c );

>If one declares an interface where no const is used at all, anyone can used
>without casts.

You don't need them here either.  None of the const params above
ever result in the need for casts.  But they do promise me that
my strings are not going to be altered by the library function.

Note that when a function *does* alter its args, they are *not*
const:

void _swab( char *src, char *dest, int n );

You might assume that the src will be unaltered, but the library
makes no promises.  In this case, if the string you are passing
as src is const, you will get a warning.

For example, in a test case:

	const char *sstr = "source";
	char dstr[6] = "     ";

	_swab(sstr, dstr, 6);

T:\ctest\test\test.c(9) : warning C4090: 'function' : different 'const' qualifiers
T:\ctest\test\test.c(9) : warning C4024: '_swab' : different types for formal and actual parameter 1
test.obj - 0 error(s), 2 warning(s)

>The only difference for API is for callbacks, function variables with 'const'
>in their arguments; and nowhere else.

This is a bit confused.  Yes, you do have to pay attention to const
when defining (and implementing) callbacks:

void qsort( void *base, size_t num, size_t width,
 int (__cdecl *compare )(const void *elem1, const void *elem2 ) );

But how does that make use of const less sensible?  Or desirable?
Not sure what you are saying here...

>PS The worst thing about 'const' in C is that few people can say when it is
>needed without consulting the Manual. C is good enough without 'const'.

Even fewer people can get the args to qsort() right without checking.
I can't.  Fortunately, in Visual Studio you have F1 readily at hand...  ;-)

-- Jeremy H. Griffith, at Omni Systems Inc.
  (jeremy@omsys.com)  http://www.omsys.com/




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS