Advertisement
Guest User

Illegal indirection and problem with malloc

a guest
Dec 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8.  
  9. int quantity = 0;
  10.  
  11.  
  12. void load_array(long int **array,FILE *output); //To load array from file
  13. void close_files(int quantity,...);
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.     FILE *input1 = NULL, *input2 = NULL, *output = NULL;  //file's pointers
  18.  
  19.     long int*array1 = NULL , *array2 = NULL; //variant with the local variables
  20.     int quantity = 3;
  21.  
  22.  
  23.     if (!(input1 = fopen(argv[1], "r")))
  24.     {
  25.         printf("Here is the error while opening input file %s!\n", argv[1]);
  26.         exit(1);
  27.     }
  28.  
  29.     if (!(input2 = fopen(argv[2], "r")))
  30.     {
  31.         printf("Here is the error while opening input file %s!\n", argv[2]);
  32.         exit(2);
  33.     }
  34.  
  35.     if (!(output = fopen(argv[3], "w")))
  36.     {
  37.         printf("Here is the error while opening output file &s !" , argv[3]);
  38.         exit(3);
  39.     }
  40.  
  41.     /*load_array(&array1,*input1);
  42.     printf("\n");
  43.     load_array(&array2,*input2);
  44.     printf("\n");*/*
  45.  
  46.     /*for (int counter = 0 ; counter < quantity; counter++)
  47.         fprintf(output,"%ld ", array1[counter]);*/
  48.  
  49.     close_files( 0, 0);
  50.  
  51.     return 0;
  52. }
  53.  
  54.  
  55. void load_array(long int **array,FILE* input)
  56. {
  57.     int counter = 0, decimal = 0 , numbers = 0 ;
  58.     char current_symbol, *temporary_buffer = NULL;/* *temporary_pointer = temporary_buffer;*/
  59.  
  60.     while (!feof(input))
  61.          {
  62.            
  63.             current_symbol = fgetc(input);
  64.             if ((current_symbol != ' ') && (current_symbol != EOF) && (current_symbol != '\n'))
  65.                {
  66.                    temporary_buffer = (char*)realloc(temporary_buffer, ++counter * sizeof(char));
  67.                    temporary_buffer[counter-1] = current_symbol;
  68.                }
  69.             if ((current_symbol == ' ')  || (current_symbol == '\n'))
  70.             {
  71.                 numbers++;//To estimate how much numbers we have
  72.                 decimal = 0;
  73.  
  74.                 int digits = 0;
  75.  
  76.                 for (int coun = 0  ; coun<counter;coun++) //To use gorner to convert string into integer
  77.                      {
  78.                          decimal = decimal * 10 + (isdigit(temporary_buffer[coun]) ? (temporary_buffer[coun] - '0') : (temporary_buffer[coun] - 'A' + 10));  
  79.                          digits++;
  80.                      }
  81.                 //???
  82.                 *array = (long int*)malloc( 100* sizeof(long int));
  83.                 //array[numbers-1] = (long int*)realloc(array[numbers - 1], sizeof(long int));
  84.                 **array++ = decimal;
  85.                 //*array++;
  86.                 free(temporary_buffer);
  87.                 temporary_buffer = NULL;
  88.                 counter = 0;
  89.             }
  90.              
  91.          }
  92.     quantity = numbers;
  93.     /*for (int counter = 0; counter < numbers; counter++)
  94.         printf("%ld ", **array);*/
  95.  
  96.     return;
  97. }
  98.  
  99. void close_files(int quantity,...)
  100. {
  101.     int counter = 0;
  102.  
  103.     va_list argument_pointer;
  104.     va_start(argument_pointer, quantity);
  105.  
  106.     while (counter++ < quantity)
  107.        {
  108.             fclose(*(va_arg(argument_pointer, FILE**)));
  109.        }
  110.  
  111.     va_end(argument_pointer);
  112.  
  113.     return;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement