home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18789 < prev    next >
Encoding:
Text File  |  1992-12-22  |  880 b   |  31 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!news.ans.net!nynexst.com!jsd
  3. From: jsd@nynexst.com (Joseph Delotto)
  4. Subject: allocating 2-d array
  5. Message-ID: <1992Dec22.185523.4732@nynexst.com>
  6. Sender: news@nynexst.com (For News purposes)
  7. Organization: NYNEX Science & Technology, Inc.
  8. Date: Tue, 22 Dec 92 18:55:23 GMT
  9. Lines: 20
  10.  
  11. Sorry if this is too simple, but I can't find a good example in
  12. any books.  I want to dynamically allocate a 2-d array for a
  13. variable defined as:
  14.  
  15. int    **myarray;
  16.  
  17. where the dimensions = nrows,ncols.  Currently I'm using the
  18. following code:
  19.  
  20. myarray = (int **) malloc ((unsigned) nrows*sizeof(int*));
  21. for(i=0;i<=nrows;i++)
  22.      myarray[i] = (int *) malloc ((unsigned) ncols*sizeof(int));
  23.  
  24. which seems to work ok but looks awfully sloppy.  Is there any
  25. way to get the whole thing done in one shot?
  26.  
  27. Thanks for any help,
  28.  
  29. Joe DeLotto
  30. jsd@nynexst.com
  31.