Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: "array" vs. "&array" ? Message-ID: <571@mwtech.UUCP> Date: 22 Dec 89 21:04:13 GMT References: <1989Dec22.013757.3086@sj.ate.slb.com> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 74 Keywords: Some days ago I posted a question if it is legal (or makes sense) to write a "&" before an array. I received a few responses, some mentioned, that an "&" before an array is illegal and/or that the compiler simply ignores it. I will not object to this - my compiler with ISCs 386/ix also ignores it - but I will present you a fragment of C-Code, which can not be compiled (without warnings) with this type of compilers. To my understanding, the following *is* a type mismatch: ----------------------------------------------------------------------- main() { char a[10], (*p)[10]; p = a; warning: illegal pointer combination, op = } ----------------------------------------------------------------------- The compiler seems to take my point of view. Then I'll try to get it right (to my understanding): ----------------------------------------------------------------------- main() { char a[10], (*p)[10]; p = &a; warning: & before array or function: ignored warning: illegal pointer combination, op = } ----------------------------------------------------------------------- Now, what's that? I've made it right, then the compiler throws out my "&" and complains that it is wrong. Of course it is wrong now, the compiler just *made* it wrong! And to all of you who want to now, why I might want to do such strange things, look at the following: ----------------------------------------------------------------------- main() { char m[20][10], (*p)[10]; p = m[0]; /* ^ to "&" or not to "&", that is the question */ /* do something with (*p)[x] -- a single char and have the possibility to increment p to point to the next group of 10 char-s */ } ----------------------------------------------------------------------- or another one: ----------------------------------------------------------------------- char (*foo())[10] { static char m[20][10] = { /* some initialization */ }; int i; /* do some calculations giving a value to i */ return m[i]; /* ^ to "&" or not to "&", that is the question */ } ----------------------------------------------------------------------- I simply can't get it right! What am I missing? Or is the compiler, that ignores the &, something missing? (OK, before you post it: I *know* that I can supress the warning with a typecast, but I think typecasts should only be used as last resort and not be necessary if you want nothing more than a perfectly legal and reasonable assignment or function return!) -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83