7 ms·
int main (int argc, char* argv[]) { (void)argc; (void)argv; struct Meow* p_cat = (struct Meow*)malloc(sizeof(struct Meow)); struct B
by bigdict 6y ago
int main (int argc, char* argv[]) {
(void)argc;
(void)argv;
struct Meow* p_cat = (struct Meow*)malloc(sizeof(struct Meow));
struct Bark* p_dog = p_cat;
// :3
return 0;
}
Why declare main that way if you are going to discard the arguments?
Why cast the malloc? This isn't C++.
- scruple 6y agoI'd like to know, too. Have never seen either in practice.
- flohofwoe 6y agoThe "pointless" cast is required for libraries that are written in the common subset of C and C++ so that they can be compiled both in a C or C++ compiler. Some people prefer to compile C code through a C++ compiler for various reasons, Microsoft has even been recommending this because their C++ compiler isn't quite as terribly outdated as their C compiler: https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an... (not that I agree with the reasons, but for a library it makes sense to not lock out people who prefer to compile C libraries as C++ when integrated into a C++ project).
- scruple 6y agoThank you! That's interesting and it makes sense.
- rootbear 6y agoThis is what you get when a person who is mostly a C++ coder (as per his bio) writes C. I cringe when I see things like struct Bark* p_dog = p_cat; instead of struct Bark *p_dog = p_cat; That weird affectation of C++ programmers putting the asterisk on the type and not on the declarator, where it belongs, makes my eyes bleed. I somewhat understand the reasoning, but I think it's a gross violation of the Law of Least Astonishment.
- bigdict 6y agoI never understood the reasoning behind this, is it that "the asterisk is part of the type, so we group it that way"? That misses the point of the C declaration syntax: you write an expression that when used on its own will recover the basic type. So the asterisk goes with the symbol name, because that's how you dereference a pointer. Further, it doesn't work if you want to declare more than one pointer like so: int* a, b; /* wrong */ int *a, *b; /* correct */
- rootbear 6y agoI think the issue is that programmers want a type "pointer to int", for example, but C doesn't directly provide that type. It has a type, int, with a modifier (asterisk) that can be applied to a declarator to make it a pointer to that type. One way to create a pointer type in C would be to declare it using typedef: typedef int * int_p; then one can write: int_p p, q, r; and declare three pointers to int with perfect clarity, whereas using the C++ style, we'd get: int* p, *q, *r; which is very confusing, or, int* p; int* q; int* r; which is very verbose. I honestly don't know how C++ programmers typically handle this situation. I have seen some code that strikes a middle ground: int * p; which is a little more clear, but doesn't address the multiple declarator situation. So why don't C++ programmers use typedef? I don't know, other than I understand Stroustrup doesn't like it (not without reason). (Edited for formatting and minor clarity corrections.)
- implicit 6y agoMost modern C++ code that I've seen restricts code to declaring a single variable per statement. It's not really a big deal because things are also always introduced at the latest possible position. Each is also typically given an initializer. I'd consider it suspicious if I were to see C++ that declared 3 uninitialized pointers back to back like this. And then you get to the codebases where the authors have chosen to embrace auto and type inference... :)
- 0xffff2 6y agoI handle it by outlawing multiple declaration on a single line. It may be more verbose, but it's much easier to read IMO. This has been a somewhat common rule in my experience.