Thread: Difference between character pointer

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    39

    Difference between character pointer

    Hi all,

    What is the difference between char *a and char * a?
    Where char * a can be used?

    Thank you in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sarathius
    What is the difference between char *a and char * a?
    The difference is that char *a has one space less than char * a. Read Stroustrup's answer to the FAQ [url=http://www2.research.att.com/~bs/bs_faq2.html#whitespace]Is ``int* p;'' right or is ``int *p;'' right?[/quote] (C++ FAQ, but applicable to C)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    53
    No difference in there declaration. Just one of C's many faults.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why is ignoring whitespace a fault?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Why is ignoring whitespace a fault?
    In this case... because it creates ambiguity. As in...
    Code:
    char *c, *s;   
    
    char* c ,s;
    If the language was more specific both lines would declare 2 pointers.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by whiteflags View Post
    Why is ignoring whitespace a fault?
    Because it's too empowering for the blackspace. Seriously though, you shouldn't ignore any space, regardless of its colour. That stuff just leads to compiler apartheid

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    In this case... because it creates ambiguity. As in...
    Code:
    char *c, *s;   
    
    char* c ,s;
    If the language was more specific both lines would declare 2 pointers.
    The language is specific. The * ties to whatever variable it is next to, not to whatever type it is next to.

    There is no more confusion there then there would be if you had the language specify it tied to the type instead of the variable. In fact, if it did tie to the type, then this line would be impossible to declare:
    Code:
    type* aptr, notaptr;
    It would be impossible to declare a pointer and a non-pointer on the same line if we had the language work as "specific" as you suggest.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    It would be impossible to declare a pointer and a non-pointer on the same line if we had the language work as "specific" as you suggest.
    Quzah.
    And that's a problem because??

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    And that's a problem because??
    Well, since this is C99 now, you couldn't do:
    Code:
    for( int x = 0, *p = &x; ... )
    Or whatever other random situation it would apply. It would be a problem now, because people are used to doing it one way, and it would be a drastic change. It wouldn't have been an issue when the language was designed, although honestly I don't see your way being any better.

    It's not any more difficult to tell someone "it applies to the variable it is by" than it is to say "it applies to the type it is next to". In fact, if we did it your way, this would generate an error:
    Code:
    type *ok, *error;
    It would have to, because it wouldn't have a type to bind to if we did it your way, and making it bind to anything it is by would be even more confusing than one or the other.

    Code:
    type *x, y;
    "Why does this only give one pointer?"
    "Because it binds to the variable not the type."

    Code:
    type* taterptr1, taterptr2;
    "Why does that give two pointers?"
    "Because it binds to the type."

    Code:
    type *taterptr1, *taterptr2;
    "Why does that give two pointers?"
    "Because it binds to the type ... or the variable?"

    It would cause more confusion than it would clear up.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wow, you shure do type fast...

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, yes I do.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between pointer and array
    By zcrself in forum C Programming
    Replies: 4
    Last Post: 04-25-2010, 09:10 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  4. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM
  5. Using a pointer as a character array
    By BubbleBoy in forum C Programming
    Replies: 8
    Last Post: 02-21-2003, 06:34 AM