daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest May 16th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. char*** tas;
  2.    
  3. void *realloc(void *ptr, size_t size);
  4. void *malloc(size_t size);
  5. void *calloc(size_t nmemb, size_t size);
  6.    
  7. tas         = (char ***)realloc(tas,sizeof(char **)*(i+1));
  8. tas[i]      = (char **)calloc(2,sizeof(char *));
  9. tas[i][0]   = (char *)calloc(lenKey+1,sizeof(char));
  10. tas[i][1]   = (char *)calloc(lenValue+1,sizeof(char));
  11. ++i;
  12.    
  13. T * p = malloc(n * sizeof(T));
  14.    
  15. free(p);
  16.    
  17. // allocate the table (the row pointers)
  18. char *** tas = malloc(m * sizeof(char**));
  19.  
  20. for (size_t i = 0; i != m; ++i)
  21. {
  22.     // allocate each row (the row cells)
  23.     tas[i] = malloc(n * sizeof(char*));
  24.     for (size_t j = 0; j != n; ++j)
  25.     {
  26.         // initialize the row cell by allocating the string
  27.         tas[i][j] = /* allocate string */
  28.     }
  29. }
  30.    
  31. for (size_t i = 0; i != m; ++i)
  32. {
  33.     for (size_t j = 0; j != n; ++j)
  34.     {
  35.         free(tas[i][j]);  // free the string
  36.     }
  37.  
  38.     free(tas[i]);         // free the row
  39. }
  40.  
  41. free(tas);                // free the table
  42.    
  43. RC  ||---1-----|----2----|  
  44.    |---1---||string11 |string12 |
  45.    |---2---||string21 |string22 |
  46.    |---3---||string31 |string32 |
  47.    | ....  ||  .....     ...... |
  48.    |-(N-1)-||str N-1.1|str N-1.2|
  49.    |---N---||stringN1 |stringN2 |
  50.    
  51. #include <unistd.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <ctype.h>
  55. #include <stdlib.h>
  56. void rellocateFunction(size_t lenKey, size_t lenValue);
  57. void firstAllocateFunction(size_t lenKey, size_t lenValue);
  58. int i=0;
  59. char*** tas;
  60.  
  61. int main(){
  62.     int j=0;
  63.     size_t length1, length2;
  64.     char* stringN1=(char *)malloc(10*sizeof(char));
  65.     char* stringN2=(char *)malloc(10*sizeof(char));
  66.     while(1){
  67.         printf("dragonsn");
  68.         printf("MAIN: Enter string[%d][0]:",j); scanf("%s", stringN1);
  69.         printf("MAIN: Enter string[%d][1]:",j); scanf("%s", stringN2);
  70.         length1 = strlen(stringN1); // You need the length of the string to pass it as an argumnet to allocation functions
  71.         length2 = strlen(stringN2); // Same as above comment
  72.         if(j==0) firstAllocateFunction(length1,length2); // We cant start with the use of realloc. So here you are initiliazing the allocation with calloc only.
  73.         else rellocateFunction(length1,length2); // When we already have entries in the table, we are going to use this function to allocate more memory for the new entries.
  74.         strcpy(tas[j][0],stringN1);  // Population of char*** at row j and col 0
  75.         strcpy(tas[j][1],stringN2);  // Population of char*** at row j and col 1
  76.         j++;
  77.         for (int c=0; c<j; c++) // Print the results
  78.         {
  79.             printf("tas[%d][0]:<%s>n",c, tas[c][0]);
  80.             printf("tas[%d][1]:<%s>n",c, tas[c][1]);
  81.         }
  82.     } // Notice the forever ongoing loop... That's to test if the code works. Cancel the execution with ctrl+c
  83.     return 0;
  84. }
  85.  
  86. void firstAllocateFunction(size_t len1, size_t len2){
  87. tas         = (char ***)calloc(1,sizeof(char **)); //One char*** pointer
  88. tas[i]      = (char **)calloc(2,sizeof(char *));   //pointin to 2 char**
  89. tas[i][0]   = (char *)calloc(len1+1,sizeof(char)); //containing len1+1
  90. tas[i][1]   = (char *)calloc(len2+1,sizeof(char)); //and len2+1 elements of sizeof(char) bytes.
  91. ++i;
  92. }
  93.  
  94. void rellocateFunction(size_t len1, size_t len2){
  95. tas         = (char ***)realloc(tas,sizeof(char **)*(i+1)); //One more char***
  96. tas[i]      = (char **)calloc(2,sizeof(char *));   ////////////////////
  97. tas[i][0]   = (char *)calloc(len1+1,sizeof(char)); // Same as above //
  98. tas[i][1]   = (char *)calloc(len2+1,sizeof(char)); ///////////////////
  99. ++i;
  100. }
  101.    
  102. char ***tas = malloc( (i+1) * sizeof *tas );
  103.    
  104. char ***tas = calloc( i+1, sizeof *tas );
  105.    
  106. char ***tas = realloc( NULL, (i+1) * sizeof *tas );
  107.    
  108. char ***tas = NULL;
  109. tas = realloc( tas, (i+1) * sizeof *tas );
  110.    
  111. tas[i] = calloc( 2 * sizeof *tas[i] );
  112.    
  113. tas[i][0] = calloc( lenKey + 1, sizeof *tas[i][0] );
  114. tas[i][1] = calloc( lenValue + 1, sizeof *tas[i][1] );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top