how to declare pseudo-matrix function argument that is not modified by function?

C

classicist

Suppose I have a function which takes an argv-style pseudo-matrix
and modifies neither the strings nor the pointers. How do I
use the const qualifier to signal my intentions w/o drawing compiler
warnings? E.g.,

int
sum_of_lengths_of_strings( const char **s, int ns )
{
int i, len = 0;
for ( i = 0; i < ns; ++i )
len += strlen(s);
return len;
}

int
main(int argc, char **argv )
{
printf( "command args len = %d\n",
sum_of_lengths_of_strings( argv, argc );
return 0;
}

This code draws a warning about incompatable types. Presumably
the compiler does this to guard against this situation:

void
get_a_const_string( const char **s )
{
*s = "better_not_modify_me";
}

However, even if we change the declaration of
sum_of_lengths_of_strings() to

int
sum_of_lengths_of_strings( const *const *s, int ns )

we still get the warning (at least w/ gcc).
Any ideas how to declare my intensions with const and not draw a
warning from the compiler?
 
M

Markus Becker

classicist said:
int
main(int argc, char **argv )
{
printf( "command args len = %d\n",
sum_of_lengths_of_strings( argv, argc );

There's a ')' missing before the ';'
This code draws a warning about incompatable types. Presumably

I bet it does not.
It's always better to post an actual error message and not something
you vaguely remember ;-)

Markus
 
T

tanmoy87544

classicist wrote:
... deleted correct analysis of nontrivial problem of declaring const
char *const * parameter when argument may need to be char ** ...
Any ideas how to declare my intensions with const and not draw a
warning from the compiler?

No, this is a nontrivial problem in C, fixed in C++. I usually ignore
it by using casts and a comment. In principle, you can define a safe
cast using a macro (I cannot obfuscate like some others, but can aspire
to greatness!)

static int dummy(); /* old style declaration and no definition! */
#define safecast(t,v) /* t const * must be a pointer to const t */ \
((void)sizeof( ((int(*)(t,const void*))dummy)(*(v),(v)) ),(t const
*const)(v))

And then safecast(const char*,argv) and get the equivalent of (const
char *const *const) argv, but get an incomprehensible errot if argv is
not either char **, const char **, const char *const * or char *const *
(or their const versions).

Such `const casts' are possible in C, but I have never used them in
practice.
 
T

tanmoy87544

(e-mail address removed) wrote:

an obfuscated macro that had an unfortunate line wrap ...
static int dummy(); /* old style declaration and no definition! */

Compilers often seem to warn about static functions which are not
defined. Changing it to an undefined extern function solves the
problem, at the expense of increasing the possibility of name
conflict. Else one can provide a dummy definition like {return 0;},
but a definition might start prompting compilers to provide gratuitous
warnings about an invalid call. Of course, a gratuitous warning about
unprototyped function is always possible.

Not using dummy, but using a null function pointer works on all
compilers I know of, and it certainly does not violate any constraint,
but I have not carefully checked whether that invokes undefined
behaviour (I vaguely remember having checked it is okay in C99, but it
has been too long...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top