const char problem

S

sugaray

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;
}
 
P

pete

sugaray said:
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.
 
D

Dan Pop

In said:
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
 
R

Richard Bos

pete said:
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
 
D

Dan Pop

In said:
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
 
P

pete

Dan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Fibonacci 0
C pipe 1
Problem with enums, const char* and structs 9
Command Line Arguments 0
const problem 1
pointer array problem? 7
compressing charatcers 35
const char* vs char const* 12

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top