Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Difference between following statements

Reply
Thread Tools

Difference between following statements

 
 
Shao Miller
Guest
Posts: n/a
 
      02-25-2013
On 2/25/2013 16:46, Keith Thompson wrote:
> Shao Miller <(E-Mail Removed)> writes:
>> On 2/25/2013 12:24, deepak wrote:
>>> Can someone please clarify difference between following two declaration?
>>>
>>> const char *p;
>>> char* const p;
>>>

>>
>> int main(void) {
>> const char (* (p));
>> char (* const (q));
>>
>> const char (* const (r));
>> char const (* const (s));
>>
>> return 0;
>> }
>>
>> In the first, the reference type is 'const char' and 'p' is an
>> unqualified pointer to that type. You can assign to 'p'.
>>
>> In the second, the reference type is 'char' and 'q' is a const-qualified
>> pointer to that type. You cannot assign to 'q'. Because there's no
>> initializer, 'q' in the example code is worse than useless.
>>
>> In the third and fourth, the reference types are the same: 'const char'.
>> 'r' and 's' are const-qualified pointers to that type. You cannot
>> assign to either of them, and without initializers, they are both worse
>> than useless.

>
> In all cases, I think you mean "referenced type" rather than
> "reference type".
>
> For a "pointer to FOO" type, "FOO" is the "referenced type".
> The term is introduced in N1570 6.2.5p20.
>
> (The phrase "reference type" could be confused with the C++ feature
> of that name, something that doesn't exist in C.)
>


Quite right; thanks! As a nit-pick, I believe that "referenced type"
was in C89/C90, then wasn't in C99, and now has returned for C11.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-25-2013
On 02/25/2013 03:05 PM, Alain Ketterlin wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) (Edward A. Falk) writes:
>
>>> const char *p;
>>> char* const p;

>>
>> In general, the 'const' applies to whatever immediately follows.

>
> s/follows/precedes/
>
> (placing it at the extreme left is a convenience, and the only case
> where it applies to what follows).
>
> [...]
>> const char * * s pointer to pointer to const char
>> char const * * s pointer to pointer to const char

>
> We agree, const applies to the char.


In both cases **s is what cannot change, and **s follows the keyword const.

>> char * const * s pointer to const pointer to char


In this case, *s is what cannot change, and *s follows the keyword const.

>> char * * const s const pointer to pointer to char


In this case, s cannot change, because it is what follows the keyword
'const'.

>> char * const * const s const ptr to const ptr to char


In this case, *s cannot change, because that's what follows the first
'const', and s itself also cannot change, because that's what follows
the second 'const'.

If you believe that the word "follows" should be replaced by "precedes",
I presume that either you disagree with my descriptions, or you apply
the rule in a different manner. Could you explain?
--
James Kuyper
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      02-25-2013
On 2/25/2013 17:20, James Kuyper wrote:
> On 02/25/2013 03:05 PM, Alain Ketterlin wrote:
>> (E-Mail Removed) (Edward A. Falk) writes:
>>
>>>> const char *p;
>>>> char* const p;
>>>
>>> In general, the 'const' applies to whatever immediately follows.

>>
>> s/follows/precedes/
>>
>> (placing it at the extreme left is a convenience, and the only case
>> where it applies to what follows).
>>
>> [...]
>>> const char * * s pointer to pointer to const char
>>> char const * * s pointer to pointer to const char

>>
>> We agree, const applies to the char.

>
> In both cases **s is what cannot change, and **s follows the keyword const.
>
>>> char * const * s pointer to const pointer to char

>
> In this case, *s is what cannot change, and *s follows the keyword const.
>
>>> char * * const s const pointer to pointer to char

>
> In this case, s cannot change, because it is what follows the keyword
> 'const'.
>
>>> char * const * const s const ptr to const ptr to char

>
> In this case, *s cannot change, because that's what follows the first
> 'const', and s itself also cannot change, because that's what follows
> the second 'const'.
>
> If you believe that the word "follows" should be replaced by "precedes",
> I presume that either you disagree with my descriptions, or you apply
> the rule in a different manner. Could you explain?
>


Reading the type as English can sometimes involve working backwards.
For the last example, 's' is a "constant pointer to a constant pointer
to a character," which is the opposite order to the syntactical elements
as they appear. This could explain the discrepancy.

--
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily," -- Richard Harter
 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      02-25-2013
Shao Miller <(E-Mail Removed)> writes:

> On 2/25/2013 17:20, James Kuyper wrote:
>> On 02/25/2013 03:05 PM, Alain Ketterlin wrote:
>>> (E-Mail Removed) (Edward A. Falk) writes:


[...]
>>>> In general, the 'const' applies to whatever immediately follows.
>>>
>>> s/follows/precedes/

[...]
>>>> char * const * s pointer to const pointer to char

>>
>> In this case, *s is what cannot change, and *s follows the keyword const.

[...]

Yes but we're talking about types, and const qualifies the type that
precedes, which is "char *". How would you call "*s" in your example?
Let's call this a "potential expression". Then, const applies to the
type that precedes and to the potential expression that follows
(ignoring other const qualifiers appearing, e.g., char const * const s).

>> If you believe that the word "follows" should be replaced by "precedes",
>> I presume that either you disagree with my descriptions, or you apply
>> the rule in a different manner. Could you explain?

>
> Reading the type as English can sometimes involve working backwards.
> For the last example, 's' is a "constant pointer to a constant pointer
> to a character," which is the opposite order to the syntactical
> elements as they appear. This could explain the discrepancy.


Exactly. The C++ faq suggest reading right-to-left to understand pointer
declarations with const.

-- Alain.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-25-2013
Shao Miller <(E-Mail Removed)> writes:
> On 2/25/2013 16:46, Keith Thompson wrote:

[...]
>> In all cases, I think you mean "referenced type" rather than
>> "reference type".
>>
>> For a "pointer to FOO" type, "FOO" is the "referenced type".
>> The term is introduced in N1570 6.2.5p20.
>>
>> (The phrase "reference type" could be confused with the C++ feature
>> of that name, something that doesn't exist in C.)

>
> Quite right; thanks! As a nit-pick, I believe that "referenced type"
> was in C89/C90, then wasn't in C99, and now has returned for C11.


It's in C90, C99, N1256, and N1570. (I presume it's in the released
2011 standard; my copy isn't handy.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-26-2013
On 02/25/2013 06:11 PM, Alain Ketterlin wrote:
> Shao Miller <(E-Mail Removed)> writes:
>
>> On 2/25/2013 17:20, James Kuyper wrote:
>>> On 02/25/2013 03:05 PM, Alain Ketterlin wrote:
>>>> (E-Mail Removed) (Edward A. Falk) writes:

>
> [...]
>>>>> In general, the 'const' applies to whatever immediately follows.
>>>>
>>>> s/follows/precedes/

> [...]
>>>>> char * const * s pointer to const pointer to char
>>>
>>> In this case, *s is what cannot change, and *s follows the keyword const.

> [...]
>
> Yes but we're talking about types, ...


No, I'm talking about expressions. Since you were talking about types,
that would explain the discrepancy. You may find it easier to think
about 'const' in terms of the type it modifies. I find it easier to
think about it in terms of the expressions that it turns into constraint
violations.

> ... and const qualifies the type that
> precedes, which is "char *". How would you call "*s" in your example?


I'd call it an lvalue expression, potentially referring to an object,
and it is precisely such an expression whose presence as the left
operand of an assignment or compound assignment expression, or as the
only operand of an increment or decrement expression, that would violate
a constraint because of the preceding 'const' keyword.
--
James Kuyper
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      02-26-2013
On Monday, February 25, 2013 11:24:09 AM UTC-6, deepak wrote:
> Hi,
>
> Can someone please clarify difference between following two declaration?
>
> const char *p;
> char* const p;
>
> Thanks,
> Deepak


In the first case, p is a pointer to a const char; you can modify p
(set it to point to a different thing), but you cannot modify
what p points to.

In the second case, p is a const pointer to a non-const char; you
can modify what p points to, but you cannot modify p (cannot set
it to point to a different thing).

Since attempting to modify a string literal invokes undefined behavior,
many people recommend declaring a pointer to it as const char *, such
as

const char *str = "string literal";

If you try to modify the contents of the literal through str, the compiler
will complain.

 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      02-26-2013
On Feb 25, 5:54*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Paul N <(E-Mail Removed)> writes:
> > On Feb 25, 5:24*pm, deepak <(E-Mail Removed)> wrote:

> <snip>
> >> Can someone please clarify difference between following two declaration?

>
> >> * const char *p;

> <snip>
> >> * char* const p;

> <snip>
> > I was going to point you to the C FAQ athttp://c-faq.com/but it
> > doesn't seem to answer this question.

>
> It's brief, but it answers it. *What do you think is missing? *(I'm
> talking of http://c-faq.com/ansi/constptrconst.html i.e. section 11.9.)


Ah, I thought it was in the FAQ somewhere, but at a quick look I
couldn't find it. As the question is about pointer declarations, one
might expect it to be either in Section 1 - Declarations and
Initializations - or Section 4 - Pointers - but it doesn't seem to
appear in either of these, even as a reference to an answer elsewhere.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      02-26-2013
On 02/26/2013 05:49 PM, Paul N wrote:
> On Feb 25, 5:54 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> Paul N <(E-Mail Removed)> writes:
>>> On Feb 25, 5:24 pm, deepak <(E-Mail Removed)> wrote:

>> <snip>
>>>> Can someone please clarify difference between following two declaration?

>>
>>>> const char *p;

>> <snip>
>>>> char* const p;

>> <snip>
>>> I was going to point you to the C FAQ athttp://c-faq.com/but it
>>> doesn't seem to answer this question.

>>
>> It's brief, but it answers it. What do you think is missing? (I'm
>> talking of http://c-faq.com/ansi/constptrconst.html i.e. section 11.9.)

>
> Ah, I thought it was in the FAQ somewhere, but at a quick look I
> couldn't find it. As the question is about pointer declarations, one
> might expect it to be either in Section 1 - Declarations and
> Initializations ...


It is - see question 1.20 (the answer cross references 11.9)
 
Reply With Quote
 
army1987
Guest
Posts: n/a
 
      03-01-2013
On Mon, 25 Feb 2013 09:24:09 -0800, deepak wrote:

> Can someone please clarify difference between following two declaration?


<nitpick>Contrary to what you seem to imply in the subject line,
declarations are not statements. (This means that, as IIRC was mentioned
somewhere recently, `if (something) int i;` is a syntax error.)</nitpick>




--
[ T H I S S P A C E I S F O R R E N T ]
Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
-- fathermckenzie di it.cultura.linguistica.italiano
<http://xkcd.com/397/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between the two following pieces of c Doney Kaka Ruby 9 11-19-2009 09:20 PM
Difference Between The 2 Statements RN1 ASP .Net 3 04-14-2008 11:33 AM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
What's the difference between these 2 statements? ATSkyWalker Python 11 04-20-2005 08:49 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM



Advertisments