const char *p == char const *p ? 
Author Message
 const char *p == char const *p ?

I looked in the faq file but didn't find the answer for this exact question.

MY QUESTION IS:

  char const *p

equivalent to

  const char *p    ?

I.e, both pointers can be modified, but can not modify the contents at
the location they are pointing to (correct?)

I  understand that
  char *const p
is a constant pointer, i.e., the pointer can not be modified, but
the contents of the location it is pointing to can.

Thanks for clearing up my question

Esmail



Fri, 05 May 1995 00:29:12 GMT  
 const char *p == char const *p ?

Quote:

> Is "char const *p" equivalent to "const char *p"?

Yes.  They are both pointers to constant character; you may
change the pointer, but not the character it points to.

Some programmers prefer "char const *" to "const char *".
They argue that C declarations are read "inside out":

   char const * p ;
                ^     p is a
              ^              pointer to
        ^^^^^                           constant
   ^^^^                                          char

But that's just a matter of style.

Jutta



Fri, 05 May 1995 09:08:20 GMT  
 const char *p == char const *p ?
|> Is "char const *p" equivalent to "const char *p"?
|
|Yes.  They are both pointers to constant character; you may
|change the pointer, but not the character it points to.
|
|Some programmers prefer "char const *" to "const char *".
|They argue that C declarations are read "inside out":
|
|   char const * p ;
|                ^     p is a
|              ^              pointer to
|        ^^^^^                           constant
|   ^^^^                                          char
|
|But that's just a matter of style.

Sorry, you're making a mistake, `const char * p' is _not_ equivalent
to `char * const p'. The first declaration reads: `there is a character
constant, and p points to it', while the second one reads: `this is
a character pointer p and it is a constant.'

Using `char * const p' without an explicit initialization is quite
silly, because assigning a value to p later on in the code, is
not allowed. I do agree that the syntax is a bit confusing ...

kind regards,




Fri, 05 May 1995 18:32:00 GMT  
 const char *p == char const *p ?

~|> Is "char const *p" equivalent to "const char *p"?
~|
~|Yes.  They are both pointers to constant character; you may
~|change the pointer, but not the character it points to.
[stuff deleted]
~
~Sorry, you're making a mistake, `const char * p' is _not_ equivalent
~to `char * const p'. The first declaration reads: `there is a character
~constant, and p points to it', while the second one reads: `this is
~a character pointer p and it is a constant.'
~

RTFQ.

The question was
Is "char const *p" equivalent to "const char *p"?

Regards

    -Alun

--
A.Champion                |  That's an interesting point, in the sense of

  *I'm as bad as the worst - but thank God(?) I am as good as the best !!*
                *I'm not modest - I'm just honest*



Sat, 06 May 1995 19:05:54 GMT  
 const char *p == char const *p ?

Quote:



>|> Is "char const *p" equivalent to "const char *p"?
>|
>|Yes.  They are both pointers to constant character; you may
>|change the pointer, but not the character it points to.
>|
>|Some programmers prefer "char const *" to "const char *".
>|They argue that C declarations are read "inside out":
>|
>|   char const * p ;
>|                ^     p is a
>|              ^              pointer to
>|        ^^^^^                           constant
>|   ^^^^                                          char
>|
>|But that's just a matter of style.
>Sorry, you're making a mistake, `const char * p' is _not_ equivalent
>to `char * const p'. The first declaration reads: `there is a character
>constant, and p points to it', while the second one reads: `this is
>a character pointer p and it is a constant.'

While Jos it correct in what he says about `char * const p', Jutta is not
making a mistake, since they're talking `const char * p'.

regs,
Graham



Tue, 09 May 1995 08:43:01 GMT  
 const char *p == char const *p ?

[ I babbled about char * const p; ]

|While Jos it correct in what he says about `char * const p', Jutta is not
|making a mistake, since they're talking `const char * p'.

Yes, Jutta was correct, I simply goofed by misreading the original question.
I'm sorry for the confusion. I realize that I mustn't post or reply when
my morning {*filter*}/coffee ratio in my veins is still high ... ;-)

kind regards,




Tue, 09 May 1995 17:32:29 GMT  
 const char *p == char const *p ?
I am new to C programming, while i was doing a numerical analysis
program which uses many for loop
here is the loop looks like

for(i=1;i<=n;i++);
{

Quote:
}

i put the ";" after the loop header, when the program is excuted, the loop
is skipped, however, it works fine when the ";" is removed.

I just want to find out why the compiler does not catch it during
compilation. It is compiled with UNIX gcc.

Thanks

C. K. koo



Tue, 09 May 1995 21:43:43 GMT  
 const char *p == char const *p ?

Quote:
>I am new to C programming, while i was doing a numerical analysis
>program which uses many for loop
>here is the loop looks like
>for(i=1;i<=n;i++);
>{
>}
>i put the ";" after the loop header, when the program is excuted, the loop
>is skipped, however, it works fine when the ";" is removed.
>I just want to find out why the compiler does not catch it during
>compilation. It is compiled with UNIX gcc.

Because it's perfectly legal, and not even guaranteed to be stupid.
-Ekr

--
-------------------------------------------------------------------------------


                "Meet the new boss, same as the old boss."



Wed, 10 May 1995 00:02:00 GMT  
 const char *p == char const *p ?

|I am new to C programming, while i was doing a numerical analysis
|program which uses many for loop
|here is the loop looks like
|
|for(i=1;i<=n;i++);
|{
|}
|
|i put the ";" after the loop header, when the program is excuted, the loop
|is skipped, however, it works fine when the ";" is removed.
|
|I just want to find out why the compiler does not catch it during
|compilation. It is compiled with UNIX gcc.

The compiler can't complain, because your code is perfectly legal
(syntactically speaking that is.) What the compiler actually `sees'
is this: `for(i=1;i<=n;i++)<<empty statement expression>>;' followed
by a block of code. The <<empty statement expression>> is allowed in C.
In this situation it might seem silly to allow this, but have a look
at a naive example implementation of strlen:

size_t strlen(char *s) {

        size_t len;

        for (len= 0; *s; len++, s++);

        return len;

Quote:
}

See? All the necessary actions are done _within_ the for loop part, i.e.
no other statements are necessary here. Isn't C wonderful! ;-)

kind regards,




Wed, 10 May 1995 02:12:34 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. const char * vs char const *

2. Help: inline char const* const& max(char const* const &a, char const* const &b)

3. Why not static const char * const __func__ ?

4. meaning of const char *const ?

5. Conversion const char* -> const unsigned short*

6. char** and const char** ...

7. va_arg(ap, const char *) = char*?

8. const char* to char[64]

9. char^ to const char* casting

10. const char ** incompatible with char ** (and vice versa)?

11. passing const char* to char* fails

12. const char* vs. char *

 

 
Powered by phpBB® Forum Software