home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!stanford.edu!agate!usenet.ins.cwru.edu!po.CWRU.Edu!wct
- From: wct@po.CWRU.Edu (William C. Thompson)
- Newsgroups: comp.lang.c
- Subject: Macros
- Date: 7 Nov 1992 07:41:56 GMT
- Organization: Case Western Reserve University, Cleveland, OH (USA)
- Lines: 42
- Message-ID: <1dfs04INNg3e@usenet.INS.CWRU.Edu>
- Reply-To: wct@po.CWRU.Edu (William C. Thompson)
- NNTP-Posting-Host: slc5.ins.cwru.edu
-
-
- I'm trying to write a macro that allocates memory for Rows double *,
- and then allocating for Cols double. Basically, A is a double **,
- which points to a row of double *, which point to a buffer of doubles.
- A[i][j] is then the jth element in the ith row. But I'm having
- trouble with this macro business. I don't want to write a function
- to do it.
-
- ALL.H
- -----
- #define malloc2d(A,rows,cols); (
- {
- int i;
-
- if (((A)=(double **)malloc(sizeof(double *)*(rows)))==NULL)
- printf("Allocation error!\n");
- for (i=0; i<(cols); i++)
- if (((A)[i]=(double *)malloc(sizeof(double)*(cols)))==NULL)
- printf("Allocation error!\n");
- }
- )
-
- TEST PROGRAM
- ------------
- #include <stdio.h>
- #include <all.h>
-
- double **A;
- int i,j;
-
- main()
- {
- malloc2d(A,50,50);
- for (i=0; i<50; i++)
- for (j=0; j<50; j++)
- A[i][j]=1.0/(i+j+1.0);
- return 0;
- }
-
- It doesn't compile, though. What gives? E-mail responses, please...
- --
- "What do you mean Star Trek isn't real!?" - anonymous
-