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;
} 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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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. ...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: d3x0xr |
last post by:
---- Section 1 ----
------
x.c
int main( void )
{
char **a;
char const *const *b;
b = a; // line(9)
|
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";
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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: 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...
| |