Path: utzoo!mnetor!uunet!hsi!mfci!root From: root@mfci.UUCP (SuperUser) Newsgroups: comp.lang.c Subject: Re: Help me cast this!: Ultrix 2.x bug Message-ID: <386@m3.mfci.UUCP> Date: 5 May 88 02:56:00 GMT References: <294@fedeva.UUCP> <1451@iscuva.ISCS.COM> <11344@mimsy.UUCP> Reply-To: karzes@mfci.UUCP (Tom Karzes) Organization: Multiflow Computer Inc., Branford Ct. 06405 Lines: 109 Keywords: pointer to array of struct Summary: Expires: Followup-To: Distribution: In article <11344@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: >context: >struct outfile (*output)[] = <>malloc(sizeof(struct outfile) * 3); >... >> output = (struct outfile **)malloc(sizeof(struct outfile) * 3); >> >>works on Ultrix 2.something .... > >... but only because of a compiler bug. Obviously DEC have >not installed Guy Harris's fixes to chkpun() in mip/trees.c. Since "type (*)[...]" and "type **" are clearly incompatible types, I consider the above example to be terrible style. However, pcc compilers don't give a warning, and I was once told that Dennis Ritchie considers it to be perfectly legal C. So even though I consider it to be brain damaged to treat * and [] as equivalent other than at the top level, I am currently under the impression that this is standard K&R C. From your posting, it sounds like Guy Harris added a warning for this case, and in fact I noticed that suns also warn. I certainly agree with the motivation for giving a warning, but I'm not convinced that it is considered correct to do so. Anyway, here's my personal view of pointers in C: % cat apt.c int a; int b[5]; int c[4][5]; int d[3][4][5]; int *pa; int (*pb)[5]; int (*pc)[4][5]; int (*pd)[3][4][5]; int *qa; int *qb; int (*qc)[5]; int (*qd)[4][5]; int x; int *px; int **ppx; int (*xx)[10]; main() { a = 1; b[2] = 2; c[2][3] = 3; d[2][3][4] = 4; /** *** Cumbersome purist style: **/ pa = &a; pb = (int (*)[5]) b; /* &b */ pc = (int (*)[4][5]) c; /* &c */ pd = (int (*)[3][4][5]) d; /* &d */ printf("%d %d %d %d\n", *pa, (*pb)[2], (*pc)[2][3], (*pd)[2][3][4]); /** *** Normal C style: **/ qa = &a; qb = b; qc = c; qd = d; printf("%d %d %d %d\n", *qa, qb[2], qc[2][3], qd[2][3][4]); /** *** Brain damaged style. I've been told that Ritchie claims this is *** legal C. If so then I consider it to be a bug in the language. **/ pa = &a; pb = (int **) b; /* brain damage */ pc = (int ***) c; /* brain damage */ pd = (int ****) d; /* brain damage */ printf("%d %d %d %d\n", *pa, (*pb)[2], (*pc)[2][3], (*pd)[2][3][4]); /** *** Example of brain damage in action: **/ x = 123; px = &x; ppx = &px; xx = &px; /* brain damage */ printf("%d %d %d\n", **ppx, **xx == (int) &x, *(int *)**xx); } % cc apt.c -o apt % apt 1 2 3 4 1 2 3 4 1 2 3 4 123 1 123 %