[comp.lang.c] What's Wrong with this C Excert

ezab066@ut-emx.UUCP (Albert Wu) (08/01/90)

The following is part of an example I saw on p.301 of Byron S.
Gottfried's Schaum's Outline Series "Programming with C", copyrighted
1990.  It doesn't compile.  Can anyone tell me what's wrong ?  I am
a total beginner in C.  Pls don't laugh.


#include <stdio.h>

#define MAXCOLS 30

main()
{
        int (*a)[MAXCOLS],(*b)[MAXCOLS],(*c)[MAXCOLS];

	.........

        *a = (int*) malloc (rows * ncols * sizeof(nt));
        *b = (int*) malloc ( nrows * ncols * sizeof(int));
        *c =(int*) malloc ( nrows * ncols * sizeof(int));
} 

jdc@naucse.cse.nau.edu (John Campbell) (08/03/90)

From article <34877@ut-emx.UUCP:, by ezab066@ut-emx.UUCP (Albert Wu):
: The following is part of an example I saw on p.301 of Byron S.
: Gottfried's Schaum's Outline Series "Programming with C", copyrighted
: 1990.  It doesn't compile.  Can anyone tell me what's wrong ?  I am
: a total beginner in C.  Pls don't laugh.
: 
: 
: #include <stdio.h>
: 
: #define MAXCOLS 30
: 
: main()
: {
:         int (*a)[MAXCOLS],(*b)[MAXCOLS],(*c)[MAXCOLS];
: 
: 	.........
: 
:         *a = (int*) malloc (rows * ncols * sizeof(nt));
:         *b = (int*) malloc ( nrows * ncols * sizeof(int));
:         *c =(int*) malloc ( nrows * ncols * sizeof(int));
: } 

Well, except for the syntax errors

         *a = (int*) malloc (nrows * ncols * sizeof(int));  /* I presume? */

and the need to define nrows and ncols, what is this supposed

cdecl says:

``declare a as pointer to array 30 of int''

Now trying to malloc at this point sounds like a real mistake.  I
sure wish I knew what the author was thinking of here. 

Anyone else?  (Karl?)
-- 
	John Campbell               jdc@naucse.cse.nau.edu
                                    CAMPBELL@NAUVAX.bitnet
	unix?  Sure send me a dozen, all different colors.

wallace@ynotme.enet.dec.com (Ray Wallace) (08/08/90)

In article <34877@ut-emx.UUCP>, ezab066@ut-emx.UUCP (Albert Wu) writes...
>The following is part of an example I saw on p.301 of Byron S.
>Gottfried's Schaum's Outline Series "Programming with C", copyrighted
>        *a = (int*) malloc (rows * ncols * sizeof(nt));
>        *b = (int*) malloc ( nrows * ncols * sizeof(int));
>        *c =(int*) malloc ( nrows * ncols * sizeof(int));
should be
        a = (int*) malloc (nrows * ncols * sizeof(int));
        b = (int*) malloc ( nrows * ncols * sizeof(int));
        c =(int*) malloc ( nrows * ncols * sizeof(int));
that is you need to remove the pointer prefix from in front of the variables
on the left side of the assignment statement.

---
Ray Wallace		
		(INTERNET,UUCP) wallace@oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet@decwrl.dec.com
---