2

Will this correctly declare one an integer and another as an pointer to an integer or will this just cause problems?

int *countPtr, count;
NSwanson7
  • 173
  • 1
  • 13
  • 1
    Woosh. What he's saying is: It does "correctly declare one an integer and another as an pointer to an integer", but it also causes problems because it's stupid and confusing that it does this. –  Oct 11 '13 at 14:24
  • http://stackoverflow.com/questions/398395/in-c-why-is-the-asterisk-before-the-variable-name-rather-than-after-the-type – Lucabro Oct 11 '13 at 14:30

5 Answers5

3

It is a valid declaration, it will compile.

However, it can lead to confusion, so it's safer to declare it separately:

int count;
int *countPtr;

For increased readability, you can keep in mind this simple rule:

Don't mix types in one declaration.

You don't want to see nightmares like int x, y, *p, t[10], i, f();.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
1

Will this correctly declare one an integer and another as an pointer to an integer?

Yes.

Will this just cause problems?

Yes. Declarations of multiple types on a single line (especially when pointers are involved as in your example) is heavily discouraged, for the obvious readability/maintenance issues.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 1
    @user2172993: I disagree with Oli - it would only be confusing if you followed the C++-style `T* p` convention, which you aren't doing in this case. For anyone who's been programming in C for more than ten minutes, it's clear which is the pointer and which isn't. – John Bode Oct 11 '13 at 14:34
  • 1
    @JohnBode: I've seen grimness like `int x, foo(), *p;` in real code. So I'll stick to my belief that mixing types on a single line is nasty ;) – Oliver Charlesworth Oct 11 '13 at 14:37
1

It is completely fine.

  • countPtr will be a pointer to int
  • count will be just regular int
Zaffy
  • 16,801
  • 8
  • 50
  • 77
1

No, this is a valid statement and will work as expected.

Writing

int* countPtr, count

would do the same thing, but can lead to confusion.

PhillipD
  • 1,797
  • 1
  • 13
  • 23
1
Will this correctly declare one an integer and another as an pointer?

Absolutely.

will this just cause problems?

Programically speaking, No. Readability-wise, Depends on how you interpret the declaration int *countPtr, count;. I read it as "integer-holder countPtr and integer count". So no problems for me.

darshandzend
  • 396
  • 4
  • 12