8 ms·
absolutely wrong. the 'type' is pointer-to-int, the 'name' is 'p', hence 'int* p'. I understand pointers quite well, thankyou....
by TickleSteve 10y ago
absolutely wrong.
the 'type' is pointer-to-int, the 'name' is 'p', hence 'int* p'.
I understand pointers quite well, thankyou....
- bonzini 10y agoAnd "int* p, q" declares to pointers to int, amirite?
- TickleSteve 10y agono. You should only ever declare one thing per line. int* p; int q;
- bonzini 10y agoOnly if you don't know how the thing actually works.
- bluetomcat 10y agoThen how about cases like these: int* a, *b; The language was intentionally designed so that the same operators with the same precedence levels are used in declarations as well as in normal expressions.
- ScottBurson 10y ago> the 'type' is pointer-to-int, the 'name' is 'p', hence 'int* p'. It would be great if the language actually worked that way, because that's the natural way to think about it. Unfortunately, it doesn't. In the simple case of a single pointer variable, we can put the star next to the type and pretend that C works the way we think, but it's a fiction.
- Gankro 10y agoI don't see how anything they said is incorrect. The type is int*, the name is p. C just has this weirdly optimized multiple value decl syntax that for some reason really wants to make it easy to declare an array of int, pointer to int, and function to int all at once instead of making it easy to declare multiple pointers to int. It seems to be a perfectly rational reaction to say "oh this was a stupid idea, let's not take advantage of it". (which is honestly a great way to approach a lot of C's features)
- jotux 10y agoC's comma operator syntax is also weird and, in my experience, many people don't really understand it that well. edit: In reference to the last line: "oh this was a stupid idea, let's not take advantage of it". It's a weird feature that generally makes the code more difficult to understand but people still use it a lot, similar to multiple declarations on a single line with pointers mixed in.
- penguinduck 10y agoMaybe you don't understand it that well because the described usage of a comma is not a comma operator (or any kind of operator).
- imtringued 10y agohttps://en.wikipedia.org/wiki/Comma_operator https://en.wikipedia.org/wiki/Comma_operator
- penguinduck 10y agoYour point? The multiple declaration syntax which is being discussed here uses comma separators, not operators.
- bluetomcat 10y agoThe comma acts as a separator in declarations, function calls and initializers. In ordinary expressions it is a normal binary operator (just like / or &&), but has the lowest precedence and simply evaluates the left-hand side, discarding the result, then (in that order) evaluates the right-hand side, returning its value as the result.
- ScottBurson 10y agoI don't really disagree; just remember that you're abusing the syntax, because if you forget, it will abuse you back.