473,320 Members | 2,146 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,320 software developers and data experts.

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 1807
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*****@mindspring.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*****@mindspring.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.individual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
pete <pf*****@mindspring.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*****@mindspring.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
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. ...
7
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...
2
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...
8
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....
24
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...
6
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...
3
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...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
20
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
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...
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...
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.