473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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[i]);
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?

Nov 15 '05 #1
3 2823
classicist <tp*@accelrys.com> schrieb:
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
Nov 15 '05 #2
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.

Nov 15 '05 #3

ta*********@gmail.com 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...)

Nov 15 '05 #4

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

Similar topics

2
by: Andrew Thompson | last post by:
I would like to create a menu that uses the 'active' pseudo-class to highlight the current page, but I cannot get it to work. The URL http://www.lensescapes.com/tst/nav/1.jsp shows the attempts...
4
by: Stephen Poley | last post by:
The issue of the focus pseudo-class came up a few weeks ago, and I finally got around to trying it out (better late than never ...) The recommended order given for the pseudo-classes is link,...
8
by: Kerberos | last post by:
As you might know, IE doesn't support the :hover pseudo-class on every tag (just the "a" tag AFAIK) Is there a simple JavaScript that would solve the problem? I have images that have a light blue...
5
by: Woon Kiat | last post by:
Hi, Using IDL, I can declare my enumeration like following, library MyAppLib { importlib("stdole32.tlb"); importlib("stdole2.tlb"); typedef enum MyColor
1
by: rudderstick | last post by:
Hi there all, I have an interesting problem.... I work for a company that develops software for the building industry and would like to distribute one of our software products via the web. ...
7
by: Irish | last post by:
Hello all :) Hopefully someone can shed some light on this problem I'm having. I'm trying to declare a variable to a type of class I've defined (which is a minHeap), but actually instantiate it...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
2
by: juan.gautier | last post by:
Hi, I try to construct a SQL code for a view to select a specific data from a table, this query take the value of the filter from a text box in a visual basic 6.0 form. my problem is when i...
3
by: bytehunter | last post by:
Hello, I hope this is the best place to ask my question. I was wondering if there was such a thing as a pseudo terminal in Windows. For those unfamiliar, in Linux/Unix one can create a pseudo...
2
by: almisba7 | last post by:
Hi, I have the following situation, can anybody help me in that? Following is a "pseudo" code of what I want to do in VB using VB.NET 2005: In my main form I have the following 'Code Begins...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.