473,826 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const char problem

I wrote a short code as shown below for experiment purpose,
It is successfully compilable, yet I found the piece I wrote
even confused myself with the consts and the asterisks, and
which brought up three questions to me:

1) will the casting in line 1 affect the rest of the
declarations ?
2) what's the difference between line 2 and line 3
3) are there any practical purposes of the usage of any
of the three ?

#include <stdio.h>

int main(int argc,char **argv)
{

const char **pa=(const char **)argv; // line 1
const char *const *p=argv; // line 2
const char *const *const pp=argv; // line 3

printf("%s %c\n",*++p,**p) ;
printf("%s %c\n",*pp,**pp) ;

return 0;
}
Nov 14 '05 #1
5 1831
sugaray wrote:

I wrote a short code as shown below for experiment purpose,
It is successfully compilable, yet I found the piece I wrote
even confused myself with the consts and the asterisks, and
which brought up three questions to me:

1) will the casting in line 1 affect the rest of the
declarations ?
No, not even if the cast had any effect at all, which it doesn't.
2) what's the difference between line 2 and line 3
pp has a const qulaifier that p doesn't have.
3) are there any practical purposes of the usage of any
of the three ?

#include <stdio.h>

int main(int argc,char **argv)
{

const char **pa=(const char **)argv; // line 1
const char *const *p=argv; // line 2
const char *const *const pp=argv; // line 3

printf("%s %c\n",*++p,**p) ;
The above line is unspecified behavior
depending on which argument is evaluated first.
printf("%s %c\n",*pp,**pp) ;

return 0;
}


--
pete
Nov 14 '05 #2
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
printf("%s %c\n",*++p,**p) ;


The above line is unspecified behavior
depending on which argument is evaluated first.


Nope, it's undefined behaviour, because the old value of p is used for
other purposes than computing its new value, with no intervening
sequence point.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
pete <pf*****@mindsp ring.com> wrote:
sugaray wrote:
printf("%s %c\n",*++p,**p) ;


The above line is unspecified behavior
depending on which argument is evaluated first.


Undefined. p gets read and modified without a sequence point in between.

Richard
Nov 14 '05 #4
In <40************ ***@news.indivi dual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
pete <pf*****@mindsp ring.com> wrote:
sugaray wrote:
> printf("%s %c\n",*++p,**p) ;


The above line is unspecified behavior
depending on which argument is evaluated first.


Undefined. p gets read and modified without a sequence point in between.

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
If that were enough for invoking undefined behaviour, i = i + 1 would
invoke undefined behaviour. The actual rule is slightly more complex.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Dan Pop wrote:

In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
printf("%s %c\n",*++p,**p) ;


The above line is unspecified behavior
depending on which argument is evaluated first.


Nope, it's undefined behaviour, because the old value of p is used for
other purposes than computing its new value, with no intervening
sequence point.


Thank you.

--
pete
Nov 14 '05 #6

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

Similar topics

3
2241
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. Someone on the SuSE programming mailing list suggested my problem is that I'm trying to execute a function (I assume he meant the constructor) at compile time. The same source code compile if I don't try to split it up into separate libraries. ...
7
4370
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
2
2503
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this section to flash memory and that would be OK. I have a structure with one member of it being const char** array;
8
2598
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
24
2354
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
6
10058
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This little bit of code: SimS::SimS (ostream &s) {
3
3228
by: dstevel | last post by:
The signature for strtol is: strtol( const char*, char**, int) So.. if we start with a passed "const char*" (pointer to const char), then we can't create a non-const char pointer pointer to that const char pointer as in: void func( const char* a ) {
10
2797
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
20
3575
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
39
2799
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I read somewhere that it when you won't modify after its initialization... So when exactly do I use one or another? Is it *wrong* not use const when I should?
0
10464
jinu1996
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...
1
10501
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
10182
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
9288
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...
1
7729
isladogs
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...
0
5767
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4403
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
2
3943
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3065
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.