home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19326 < prev    next >
Encoding:
Text File  |  1993-01-06  |  1.3 KB  |  54 lines

  1. Path: sparky!uunet!stanford.edu!agate!darkstar.UCSC.EDU!cats.ucsc.edu!banshee
  2. From: banshee@cats.ucsc.edu (Wailer at the Gates of Dawn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP Please Please (malloc on an array)
  5. Date: 6 Jan 1993 21:09:41 GMT
  6. Organization: University of California, Santa Cruz
  7. Lines: 41
  8. Distribution: World
  9. Message-ID: <1ifhqlINNjap@darkstar.UCSC.EDU>
  10. References: <Matt.15.726288203@chchp.ac.nz>
  11. NNTP-Posting-Host: am.ucsc.edu
  12.  
  13.  
  14. Matt@chchp.ac.nz (Matt Thompson) writes:
  15.  
  16.  
  17. >Code using standard char array
  18. >------------------------------
  19. >main()
  20. >{
  21. >char array1[1000][11];
  22. >int temp;
  23.  
  24. >for(temp=0;temp<999;temp++)
  25. >   sprintf(array1[temp],"Test %d",temp);
  26.  
  27. >printf("\n\nEnter a number :");
  28. >scanf("%d",&temp);
  29. >printf("\n%s",array1[temp]);
  30. >}
  31.  
  32. >Code using malloc etc
  33. >---------------------
  34. main()
  35. {
  36.     char    *array1[1000];
  37.     int      i;
  38.  
  39.     for (i = 0; i < 999; i++) {
  40.         array1[i] = malloc(11);
  41.         sprintf(array1[i], "Test %d", i);
  42.     }
  43.  
  44.     printf("\n\nEnter a number: ");
  45.     scanf(" %d", &i);
  46.     printf("\n%s\n", array1[i]);
  47.  
  48. -- 
  49. The Wailer at the Gates of Dawn              | banshee@cats.UCSC.EDU       |
  50. Just who ARE you calling a FROOFROO Head?    |                             |
  51. DoD#0667  "Just a friend of the beast."      | banshee@ucscb.UCSC.EDU      |
  52. 2,3,5,7,13,17,19,31,61,89,107,127,521,607....| banshee@ucscb.BITNET        |
  53.