Skip navigation

This seems to be a trivial and is an inherently subjective question but I want to know what the best practice is __with reasons__.

I used to write

    char * var ;

But I started writing

    char* var ;

Because I thought that the type of var is simply char* so the * pointer should be stuck to the type so its read in “one eyeful.”

THEN I ran into a bug recently where I didn’t notice that I had declarations

    char* var1, var2 ;

And I actually didn’t realize that var2 was type char, not char*. Hmm. Then I thought, well, this way makes the most sense then:

    char *var1, *var2 ;

Currently I’m thinking the last way is the correct way now.

Advertisements

4 Comments

  1. I never coded like any of the three but the last one, I don’t know, but maybe because I’ve been raised around the kernel space!, which most if not all code have it as ‘char *var1’ instead of the other ways, it makes more sense anyways.

  2. I understand why you changed your mind after the char* var1, var2 thing, but consider this:

    typedef CharPtr char*;

    CharPtr is now alias of type char* … So I think there is reasons to both ways. Personally, I use char* and int&, but I started out using char *p and int &i… I have also used char * p and int & i though… Funny how it matters to us programmers. I think you should just stick with the one you like the most/think is more readable/pretty. There’s no right or wrong here, at least I don’t think so.

  3. Sorry, ‘there ARE reasons to both ways’* … My english has worsened!

  4. Its ok, just use code ;)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: