Re: Pointers

Jens.Toerring_at_physik.fu-berlin.de
Date: 03/08/05


Date: 8 Mar 2005 16:02:51 GMT

ByteSurfer <bytesurfer@gmail.com> wrote:
> some times is am kinda confused.
> they like to code

> char* a;
> char *a;

> is that both are of the same ??

Yes, it doesn't matter if you have white space before or after the '*'.
There's also the third and fourth possibility:

char * a;
char*a;

That shows it doesn't matter at all where and how much white space
you put before ot after the asterisk.

Some people prefer 'char*' to 'char *' because they feel that it
makes it more obvious that the variable to be defined is a pointer,
while others don't like it because they feel that it makes it easier
to overlook that in

char* a, b;

'b' isn't a pointer but a simple char variable despite the '*' after
the 'char'.

> and some more is it :

> *const char a;
> -> a pointer of a constant address to a character

No, that's a syntax error. You can't have a '*' at the start when
defining a variable.

> const char* a; or const char *a;
> -> a constant value of a char.

No, that's a pointer to an array of chars with the attached promise
that you won't change the contents of what the pointer points to.
This is quite useful when you use pointers that point to literal
strings, i.e.

const char *my_hello = "Hello, boys and girls!";

because the compiler may be able to gently remind you you're doing
something stupid when you don't keep your promise, or when you use
this for function arguments to indicate that the function is not
going to change the elements of the array it receives. An example is

   char *strcpy( char *dest, const char *src );

This makes it immediately clear that strcpy() will only change the
destination string, but will leave the source string alone. And
again it doesn't matter if you have white space before or after
the '*' or before and after the asterisk or none at all.

> then how bout *const char* a;
> will the spaces char* and char * the same ??

That's also a syntax error.
                                   Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Re: Question about void pointers
    ... it's a matter of whether you prefer void * or char *. ... How do you come up with "pointer ... OH MY GOD what an idiot. ...
    (comp.lang.c)
  • Re: Comment on trim string function please
    ... char *p1, *p2, ch; ... The order doesn't matter if the test against p is trivial:) I'd like ... Yes, even p1 doesn't equal to NULL, it's not a valid pointer ... Here you can still generate in invalid pointer. ...
    (comp.lang.c)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: when is typecasting (unsigned char*) to (char*) dangerous?
    ... > When are they not consistent? ... patterns that are valid as `unsigned char' might be invalid ... treated as "trap representations" and could cause your program ... Given a pointer to any data object, ...
    (comp.lang.c)