-4

I am pretty new to c and I was review some code. I came across this:

static char * fromDataType;
static char * toDataType;
static char * fromRank;
static char * toRank;
static char * fromMethod;
static char * toMethod;
static char * fromAction;
static char * toAction;

I was wondering if char* a and char * a and char *a the same?

chrk
  • 4,037
  • 2
  • 39
  • 47

4 Answers4

5

White space is not significant in C source, except as a token separator. Spaces are not always required to separate tokens.

In this particular case, one space is required somewhere in between char and a to separate them. Any additional spaces are not semantically meaningful.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 5
    Well, not completely true. `a+++a` and `a+ ++a` mean different things. – nneonneo Aug 12 '14 at 17:45
  • Why is that _one space_ required? Isn't `char*a;` valid? – mafso Aug 12 '14 at 18:23
  • @mafso: Perhaps, but the spec is so opaque to me that I couldn't find the place that said that. I've never actually seen it written that way. – Robert Harvey Aug 12 '14 at 18:25
  • @mafso I think Robert was referring to `char a` needs one space. – Drew McGowen Aug 12 '14 at 18:27
  • That is what I was referring to. Not sure if the tokenizer considers `*` to be a valid replacement for that required space. – Robert Harvey Aug 12 '14 at 18:32
  • Ah, OK. Thought, there is another peculiarity of C I didn't know :). And yes, I haven't seen it either (and I assume most agree that this looks ugly). FYI, if understand the spec correctly (C11 6.4.6 and 6.7.6 p.1), it's allowed. – mafso Aug 12 '14 at 19:06
2

They are just slightly different ways to write EXACTLY the same thing.

1

char *a, char * a and char *a is same.
But take the case of
(i) char *a,b
(ii) char * a,b
In the second case it gives the impression that both a and b are of char pointers

mani deepak
  • 401
  • 1
  • 4
  • 15
0

Although char* a, char * a and char *a (and, indeed, char*a) all mean the same thing, it may be possible to start a small religious war by suggesting that char* a is the one true way, and all others are abominations, heresy, unclean, etc. Equally, since char *a was good enough for K&R, then all other forms betray clue < 0 (as we say in Cucamonga).

  • `char *a` reflects how the grammar actually works; it's always parsed as `char (*a)`. That is, the type of the *expression* `*a` is `char`. I understand why some people write `char* a` (it emphasizes the type of the *object* `a`), but it leads to mistakes like writing `char* a, b` and believing both `a` and `b` are pointers. – John Bode Aug 12 '14 at 20:53
  • @JohnBode: I could agree that following the grammer for types is clearer, if that grammer wasn't quite so ineffably obscure ! The obscurity of the grammer is self-evident, but I offer a small example: everyone knows `const char *a` (read from the back) "pointer to char which is constant". But if you write `const char const *a` expecting "pointer which is constant to char which is constant", then you will be disappointed. In fact, `char const* a`, "pointer to constant char", is closer to the grammer, and hence `char const* const a`, which *is* a "constant pointer to constant char" :-( –  Aug 13 '14 at 00:27