Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Difference between following statements (http://www.velocityreviews.com/forums/t958051-difference-between-following-statements.html)

deepak 02-25-2013 05:24 PM

Difference between following statements
 
Hi,

Can someone please clarify difference between following two declaration?

const char *p;
char* const p;

Thanks,
Deepak

Ben Bacarisse 02-25-2013 05:36 PM

Re: Difference between following statements
 
deepak <deepakpjose@gmail.com> writes:

> Can someone please clarify difference between following two declaration?
>
> const char *p;
> char* const p;


http://c-faq.com/ansi/constptrconst.html

--
Ben.

Paul N 02-25-2013 05:39 PM

Re: Difference between following statements
 
On Feb 25, 5:24*pm, deepak <deepakpj...@gmail.com> wrote:
> Hi,
>
> Can someone please clarify difference between following two declaration?
>
> * const char *p;


This means p can change, but you can't use it to change the character
pointed at. For example:

p = r; // works if r is a suitable pointer
x = *p; // works if x is a suitable variable
*p = x; // won't work

> * char* const p;


This means you can't change p, but you can use it to change the
character pointed at. For example:

p = r; // doesn't work
x = *p; // works if x is a suitable variable
*p = x; // also works if x is a suitable variable

I was going to point you to the C FAQ at http://c-faq.com/ but it
doesn't seem to answer this question. It's still worth a read though.

Hope that helps.
Paul.

Ben Bacarisse 02-25-2013 05:54 PM

Re: Difference between following statements
 
Paul N <gw7rib@aol.com> writes:

> On Feb 25, 5:24*pm, deepak <deepakpj...@gmail.com> 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 at http://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.)

--
Ben.

Keith Thompson 02-25-2013 06:35 PM

Re: Difference between following statements
 
deepak <deepakpjose@gmail.com> writes:
> Can someone please clarify difference between following two declaration?
>
> const char *p;


declare p as pointer to const char

> char* const p;


declare p as const pointer to char

Source: http://cdecl.org/

Or you can install and use the "cdecl" command-line tool:

$ cdecl
Type `help' or `?' for help
cdecl> explain const char *p;
declare p as pointer to const char
cdecl> explain char* const p;
declare p as const pointer to char
cdecl>

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"

James Kuyper 02-25-2013 06:40 PM

Re: Difference between following statements
 
On 02/25/2013 12:39 PM, Paul N wrote:
> On Feb 25, 5:24�pm, deepak <deepakpj...@gmail.com> wrote:
>> Hi,
>>
>> Can someone please clarify difference between following two declaration?
>>
>> � const char *p;

>
> This means p can change, but you can't use it to change the character
> pointed at. For example:
>
> p = r; // works if r is a suitable pointer
> x = *p; // works if x is a suitable variable
> *p = x; // won't work
>
>> � char* const p;

>
> This means you can't change p, but you can use it to change the
> character pointed at. For example:
>
> p = r; // doesn't work
> x = *p; // works if x is a suitable variable
> *p = x; // also works if x is a suitable variable
>
> I was going to point you to the C FAQ at http://c-faq.com/ but it
> doesn't seem to answer this question. It's still worth a read though.


It's not quite correct to say that you can't do those things, and the
section of the FAQ that Ben referred doesn't go into the relevant issues
either. It would be more accurate to say that any attempt to do those
things is a constraint violation, which requires a diagnostic message.
Making that message mandatory is the main purpose for using 'const' in a
declaration. If you know that a given pointer should not be used to try
to change something, you can insert 'const' in the appropriate part of
the declaration to get the compiler to warn you if your code actually
does attempt to misuse it in that fashion. Either the attempt to change
the corresponding object was a mistake, or the belief that the variable
should not be used to make such changes was a mistake - you'll have to
investigate to determine which one was wrong. But at least, with the
message, you'll know there was a conflict.

However, after having issued a diagnostic, a compiler is still free to
translate your code into a program. If you choose to execute that
program, it's behavior is undefined. That means, among other
possibilities, that the program might in fact change something it should
not have changed (even objects defined as 'const').

You can disable the diagnostic message by casting the pointer to an
appropriate non-const type. Never do this unless you're sure it's both
necessary and correct - consider whether it would be more appropriate to
remove the 'const' from the declaration, rather than casting it away.

It might be perfectly safe to write code that bypasses those warnings
using a cast. However, one thing a cast cannot do for you is allow you
to safely change the contents of an object which is itself defined as
'const' (rather than, for instance, a pointer to 'const'). The behavior
is always undefined if your code attempts that,
--
James Kuyper

Shao Miller 02-25-2013 07:22 PM

Re: Difference between following statements
 
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.

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

Cheerily," -- Richard Harter

Edward A. Falk 02-25-2013 07:40 PM

Re: Difference between following statements
 
In article <3532cc18-b349-48ef-9728-cc27ffc40ada@googlegroups.com>,
deepak <deepakpjose@gmail.com> wrote:
>Hi,
>
>Can someone please clarify difference between following two declaration?
>
> const char *p;
> char* const p;


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

From my notes:

const int x = 44; x cannot change
const int *z; *z cannot change
int * const z; z cannot change
const int *const z; z cannot change and *z cannot change

char **s; pointer to pointer to char
const char * * s pointer to pointer to const char
char const * * s pointer to pointer to const char
char * const * s pointer to const pointer to char
char * * const s const pointer to pointer to char
char * const * const s const ptr to const ptr to char

const struct employee {
char *name;
int birthdate;
int job_code;
} a,b; no member or a or b can change

struct employee c,d; but c and d are not const

struct employee {
char *name;
const int birthdate; this member cannot change
int job_code;
};


--
-Ed Falk, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/

Alain Ketterlin 02-25-2013 08:05 PM

Re: Difference between following statements
 
falk@rahul.net (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.

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


and, for completeness, the twins:

const char * const * const s
char const * const * const s

where everything is const.

-- Alain.

Keith Thompson 02-25-2013 09:46 PM

Re: Difference between following statements
 
Shao Miller <sha0.miller@gmail.com> 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.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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"


All times are GMT. The time now is 07:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.