473,810 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_str ing( const char **s )
{
*s = "better_not_mod ify_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 2849
classicist <tp*@accelrys.c om> 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,cons t 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 incomprehensibl e 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*********@gma il.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
2830
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 so far, there are links to 3 pages, but (in IE6 and Mozilla?on XP) do not render the link of the current page as yellow on blue. What am I doing wrong, or is my understanding wrong?
4
4509
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, visited, focus, hover, active. However: - Mozilla doesn't seem to do anything with the active rule; - IE gets things wrong as usual: it uses the active rule for focus; it ignores the focus rule; - Opera ignores both focus and active rules and...
8
6999
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 outline, and when I pass the mouse over, the outline turns to red. #featured img { display: block; border: 1px solid #DCDFE8; padding: 1px;
5
5103
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
1782
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. here is the problem
7
3170
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 later when I get a command from the user. This is my class and if you look down a lil you can see what I'm trying to do in my main(). I want to ask for the size from the user, but when I try to I can't get my code to compile. Here is the class...
70
6291
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 using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
2
13460
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 DECLARE the variable @AMP receive a error message that said "The Declare cursor SQL construct or statement is not supported." What i do wrong? Please help me. i attach the code. DECLARE @AMP int;
3
4776
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 terminal which to an application looks and acts like a regular com port (tty device in Linux-ease). The purpose of such a thing is to allow an application which communicates via a com port to remain the same (except for changing its com port...
2
1627
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 Dim vCurrentForm As Form Private Sub CallCurrentForm (ByVal pForm as String)
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.