Advertisement
alansam

Fill string buffers.

Dec 1st, 2022 (edited)
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.58 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define STR_LEN 255
  5. #define STR_CT 20
  6.  
  7. static
  8. char const * samples[STR_CT] = {
  9.   "This is string number:  1",
  10.   "This is string number:  2",
  11.   "This is string number:  3",
  12.   "This is string number:  4",
  13.   "This is string number:  5",
  14.   "This is string number:  6",
  15.   "This is string number:  7",
  16.   "This is string number:  8",
  17.   "This is string number:  9",
  18.   "This is string number: 10",
  19.   "This is string number: 11",
  20.   "This is string number: 12",
  21.   "This is string number: 13",
  22.   "This is string number: 14",
  23.   "This is string number: 15",
  24.   "This is string number: 16",
  25.   "This is string number: 17",
  26.   "This is string number: 18",
  27.   "This is string number: 19",
  28.   "This is string number: 20",
  29. };
  30.  
  31. void load(size_t sz, char * strings);
  32. void show(size_t sz, char const * strings);
  33.  
  34. /*
  35.  *  MARK: main()
  36.  */
  37. int main(int argc, char const * argv[]) {
  38.   char * strings = malloc(STR_LEN * STR_CT * sizeof(char));
  39.   if (strings != NULL) {
  40.     load(STR_CT, strings);
  41.     show(STR_CT, strings);
  42.     free(strings);
  43.   }
  44.   else {
  45.     fprintf(stderr, "Unable to allocate working storage\n");
  46.   }
  47.  
  48.   return 0;
  49. }
  50.  
  51. /*
  52.  *  MARK: load()
  53.  */
  54. void load(size_t sz, char * strings) {
  55.   char * string = strings;
  56.   for (size_t s_ = 0ul; s_ < sz; ++s_) {
  57.     sprintf(string, "%s", samples[s_]);
  58.     string += STR_LEN;
  59.   }
  60. }
  61.  
  62. /*
  63.  *  MARK: show()
  64.  */
  65. void show(size_t sz, char const * strings) {
  66.   char const * string = strings;
  67.   for (size_t s_ = 0ul; s_ < sz; ++s_) {
  68.     printf("%s\n", string);
  69.     string += STR_LEN;
  70.   }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement