Here's a sample function which converts a string to all uppercase:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
--
Frederick Gotham 15 1655
On 2006-07-05, Frederick Gotham <fg*******@SPAM .comwrote:
>
Here's a sample function which converts a string to all uppercase:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.
>
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
I think that's okay. It's still best to be clear:
while (*p = toupper ((unsigned char) *p))
*p++;
While that's terrible style IMO, there's no ambiguity.
--
Andrew Poelstra <http://www.wpsoftware. net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
On 2006-07-05, Andrew Poelstra <ap*******@wpso ftware.netwrote :
On 2006-07-05, Frederick Gotham <fg*******@SPAM .comwrote:
>> Here's a sample function which converts a string to all uppercase:
#include <assert.h> #include <ctype.h>
void StringUp( char *p ) { do assert( *p >= 0 ); while( *p = toupper( *p ), *p++ ); }
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.
Never mind; I read your other post explaining your logic. You should
probably put a comment in there.
(For others' benefit, the test is because casting to unsigned char is more
of a bandaid than a solution.)
--
Andrew Poelstra <http://www.wpsoftware. net/projects/>
To email me, use "apoelstra" at the above address.
"You people hate mathematics." -- James Harris
Frederick Gotham <fg*******@SPAM .comwrites:
Would the "Sequence point rule" be violated if the code were changed to the
following:
[...]
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
Yes. There is no sequence point intervening between the
modification of p (in *p++) and its use (in *p). Although a
function call contains a sequence point, the compiler may elect
to evaluate the left side of the assignment and the function
argument before invoking the function call.
Furthermore, toupper may be (and often is) implemented as a macro
that does not contain the same sequence point that the equivalent
function does.
--
"The fact that there is a holy war doesn't mean that one of the sides
doesn't suck - usually both do..."
--Alexander Viro
Frederick Gotham wrote:
Here's a sample function which converts a string to all uppercase:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Would the "Sequence point rule" be violated if the code were changed to the
following:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p++ = toupper( *p ) );
}
Yes, as would be:
while (*p = toupper(*p++) );
Robert Gamble
Andrew Poelstra wrote:
On 2006-07-05, Andrew Poelstra <ap*******@wpso ftware.netwrote :
On 2006-07-05, Frederick Gotham <fg*******@SPAM .comwrote:
>
Here's a sample function which converts a string to all uppercase:
#include <assert.h>
#include <ctype.h>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
Why is that assert() there? I can see no use for it. Why is the do there?
There's no use for any of that line.
Never mind; I read your other post explaining your logic. You should
probably put a comment in there.
Which post would that be ?
Andrew Poelstra said:
On 2006-07-05, Andrew Poelstra <ap*******@wpso ftware.netwrote :
>On 2006-07-05, Frederick Gotham <fg*******@SPAM .comwrote:
>>> Here's a sample function which converts a string to all uppercase:
#include <assert.h> #include <ctype.h>
void StringUp( char *p ) { do assert( *p >= 0 ); while( *p = toupper( *p ), *p++ ); }
Why is that assert() there? I can see no use for it. Why is the do there? There's no use for any of that line.
Never mind; I read your other post explaining your logic. You should
probably put a comment in there.
(For others' benefit, the test is because casting to unsigned char is more
of a bandaid than a solution.)
But the test is deeply flawed and the cast is a perfectly reasonable
solution to the problem of toupper requiring a character representable as
unsigned char. It may be a band-aid, but it's a highly effective band-aid.
In fact, I can only think of one case where it won't work - and that is the
case where sizeof(int) is 1, which is well-known to be an
assumption-bending situation in lots of other ways too. The reason it
causes problems with toupper is that it offers no way to distinguish
between (char)-1 and (int)-1. Casting doesn't help in such a case.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Robert Gamble wrote:
Yes, as would be:
while (*p = toupper(*p++) );
Robert Gamble
This is more similar to i = i++; that is stated in the FAQ, URL
<http://www.c-faq.com/ansi/experiment.html .
lovecreatesbeau ty
Robert Gamble <rg*******@gmai l.comwrote:
>
Yes, as would be:
while (*p = toupper(*p++) );
My reading of the standard suggests that this (whether the above is
undefined behaviour) can't be decided from the standard alone since an
implementation may implement toupper as a function (rather than as a
macro /and/ a function). A minor change:
while (*p = (toupper)(*p++) );
makes it well-defined (modulo the signedness of p) for any conforming
implementation.
Of course, it is quite reasonable to lump all constructs that an
implementation /may/ render undefined as undefined but there is, I think,
a technical difference.
--
Ben.
Working towards a posting to comp.lang.c that is 100% correct.
Frederick Gotham wrote:
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = toupper( *p ), *p++ );
}
I appreciate that your are experimenting with the language but I feel a
word of advice is in order. Don't EVER CODE LIKE THAT in fielded code.
Be very explicit about what you want and let the optimizer worry about
things like that.
Also your assert is kinda useless.
Just write it as
while (*p) { *p = toupper(*p); ++p; }
And be done with.
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sensorflo |
last post by:
After browsing though many newsgroups articels I'm still not shure how
operator precedence, operator associativity, sequence points, side
effects go together. Currently I have the following view:
An expression
a = b() + c() * d++;
can be transformed with the rules of operator associativity and
operator precedence into a tree
|
by: Jeremy Chapman |
last post by:
Populating a typed dataset from xml document:
I created an xml schema (attached below), generated a typed dataset from it,
and then programatically I tried to populate the typed dataset by calling
its ReadXml method. I keep getting a constraint exception. I have
validated that my xml matches the schema using xmlspy.
I've included the schema, xml, code and exception information below. I
can't figure out why I would get the exception...
|
by: sugaray |
last post by:
Can somebody explain to me what is sequence point ?
With few examples would be even better. Thanx for your help.
|
by: Deniz Bahar |
last post by:
I know the basic definition of a sequence point (point where all side
effects guaranteed to be finished), but I am confused about this
statement:
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored."
Can someone give me examples of expressions that "barely"...
|
by: akarl |
last post by:
Hi all,
Why do I get a warning from gcc with the following program?
$ cat test.c
#include <stdio.h>
int f(int n)
{
return n;
| |
by: Kenneth Brody |
last post by:
(From something brought up on "Help with array/pointer segmentation
fault needed" thread.)
Is "?" a sequence point?
Or, more directly, is the following defined?
/* Will "ptr" be guaranteed to have been assigned before the "?"
* part is evaluated?
*/
|
by: Frederick Gotham |
last post by:
Here's a sample function which converts a string to all uppercase:
#include <cassert>
#include <cctype>
void StringUp( char *p )
{
do assert( *p >= 0 );
while( *p = std::toupper( *p ), *p++ );
}
|
by: John Smith |
last post by:
I've been playing with splint, which returns the following
warning for the code below:
statlib.c: (in function log_norm_pdf)
statlib.c(1054,31): Expression has undefined behavior (left
operand uses errno,
modified by right operand): (log(x) - mu) * (log(x) - mu)
Code has unspecified behavior. Order of evaluation of function
parameters or
subexpressions is not defined, so if a value is used and
|
by: ais523 |
last post by:
The program excerpt
int i;
char c;
char* a= {"abc","def","ghi"};
/* ... */
i=0;
c=a;
obviously invokes undefined behaviour, because i is modified twice
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |