in my function OpenData, it corrupts error on memory allocation when I compile and run it.

in main, i need to creat an int array and pass it to other function lator.. I take 4 command-line arguments ie, a.out 7 100 lychrels.dat
7 is seed number
100 is number of turn.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stringtoll.h"

#define TRUE 1
#define FALSE 0
#define SIZE 20

void OpenData(int ** arrayPtr, char file, int *arraySizePtr );
int CheckLych(int array[], int n, int count);
void ReverseAndAdd(long long num, int* timesPtr); 
int IsPalindrome (char *string, int left, int right);
void LongLongToString(long long num, char *stringPtr);
void PrintGreeting();


int main(int argc, char*argv[]) 
{
   int *array, randomNb, arraySize;
   int i, judge, iteration = 0;
   
   if (argc !=4)
   {
      fprintf (stderr, "This program requires following command");
      fprintf (stderr, " line arguments\n");
      fprintf (stderr, "'seed number', number of 'values' to be tested, ");
      fprintf (stderr, "and a 'file name'.\n");
      exit(-1);
   }
   
   OpenData(&array, argv[3], &arraySize);
  
   int numFiles, seed;
   
   numFiles = atoi(argv[2]);
   seed = atoi(argv[1]);
   
   for (i=0; i <numFiles; i++)  
   {
      srand(seed);
      randomNb = rand()%2001;
      
      judge = CheckLych(array, randomNb, arraySize);
      printf (" NUM = %d\n", randomNb);
      if (judge == TRUE)
      {
         fprintf(stderr, "This problem couldn't be solved: %d is a lychrel.\n", randomNb);
      }
      else
      {     
         ReverseAndAdd(randomNb, &iteration);
         
      }      
   }
   return 0;
}




void OpenData(int ** arrayPtr, char file, int *arraySizePtr )
{
   FILE *ifp;
   int n;
   int i;
   ifp = fopen(file, "r");
   
   if(ifp == NULL)
   {
      fprintf(stderr, "can't open %s ... ...\n", file);
      exit(-2);
   }
   
  while(fscanf(ifp, "%d", &n) != EOF)
   { 
      *arraySizePtr++;
   }
   
   rewind(ifp);
   
   *arrayPtr = (int *) malloc (*arraySizePtr * sizeof(int));
   if (*arrayPtr == NULL)
   {
      fprintf(stderr, "Memory allocation failed... ...\n");
      exit (-3);
   }
   
   for (i=0; i<*arraySizePtr; i++)
   {
      fscanf(ifp, "%d", *arrayPtr[i]);   
   }
   fclose(ifp);
}
void LongLongToString(long long num, char *stringPtr)
{
   int counter = 0, i;
   int copyNum;
   
   copyNum = num;
   while(num !=0)
   {
      num = num / 10;
      counter ++; 
   } 
   
   for(i = counter; i>=1; i--)
   {
      stringPtr[i-1] = (copyNum % 10)+ '0';    
      copyNum = copyNum / 10;
   }
   stringPtr[counter] = '\0';
}

void ReverseAndAdd(long long num, int* timesPtr)
{ 
   char reverseString[SIZE];
   char string[SIZE];
   long long copyNum;
   int i= 0;
   long long reverseNum, total;
   
   int length;
   int boolean;
   int iteration;
   
   iteration = *timesPtr;
   
   LongLongToString(num, string);
   length = strlen(string);
   boolean = IsPalindrome(string, 0, length);
   
   if (boolean == TRUE)
   {
      printf("This problem is solved in %d iteration(s).\n\n", iteration);
   }
   else if (boolean == FALSE)
   {
      copyNum = num;
      while(num !=0)
      {
         reverseString[i] = (num %10)+'0';
         num = num / 10;
         i++;
      }
      reverseNum = StringToLongLong(reverseString); 
      total = num + reverseNum;
      printf("%lld + %lld = %lld\n", num, reverseNum, total);
      *timesPtr ++;
         
      ReverseAndAdd(total, &iteration);
   }
}


int IsPalindrome (char* string, int left, int right)
{
   if (string == string)
   {
      return IsPalindrome(string, left+1, right-1);
   }
   if (string != string)
   {
      return FALSE;
   }
   if (left >= right)
   {
      return TRUE;
   }
}
int CheckLych(int array[], int n, int count)
{
   int counter = 0;
   int i;
   for (i=0; i<count; i++)
   {
      if (n == array[i])
      {
         counter = TRUE;
      }
   }
   return counter;
}
Ancient Dragon commented: Thanks for using code tags. +21

The second parameter to OpenData() should be char* not simply charf void OpenData(int ** arrayPtr, const char* file, int *arraySizePtr ) Didn't your compiler give you a bunch of errors because of that? There are several other syntax erorrs too, and in some cases you need to add typecasts.

then...
ifp = fopen(file, "r");

should be

ifp = fopen(*file, "r");
??

fscanf(ifp, "%d", *arrayPtr); <<< this one has invalid type argument of 'unaray *'...
what is that?

>>ifp = fopen(*file, "r");
NO, NO, NO. Just leave that line like it is because the first parameter to fopen is const char*.

>>this one has invalid type argument of 'unaray *'...
don't know -- my compiler made no complaints about that line.

here is my correction...

void OpenData(int *arrayPtr, const char* file, int *arraySizePtr )
{
   FILE *ifp;
   int n;
   int i;
   ifp = fopen(file, "r");
   
   if(ifp == NULL)
   {
      fprintf(stderr, "can't open %s ... ...\n", file);
      exit(-2);
   }
   
  while(fscanf(ifp, "%d", &n) != EOF)
   { 
      *arraySizePtr++;
   }
   
   rewind(ifp);
   
   arrayPtr = (int *) malloc (*arraySizePtr * sizeof(int));
   if (arrayPtr == NULL)
   {
      fprintf(stderr, "Memory allocation failed... ...\n");
      exit (-3);
   }
   
   for (i=0; i<*arraySizePtr; i++)
   {
      fscanf(ifp, "%d", arrayPtr[i]);   
   }
   fclose(ifp);
}

in line 16 i have Warning: value computed is not used.

you should have left the first parameter to that function alone -- it has to be double stars so that it can change the pointer that was declared in main() -- you had that one right in the first post and now you've broken it.

Another approach is to delete the first parameter altogether and just return it

char* OpenData(const char* file, int *arraySizePtr )
{
    char* arrPtr = 0;
    ...
     ...
    return arrptr;
}

int main()
{
    char* arrPtr = OpenData(argv[3], &arraySize);
    ...
}

okay, final check!

also, In main, when i Pass the array to this function, is it correct? judge = CheckLych(array, randomNb, arraySize);

void OpenData(int **arrayPtr, const char* file, int *arraySizePtr )
{
   FILE *ifp;
   int n;
   int i;
   ifp = fopen(file, "r");
   
   if(ifp == NULL)
   {
      fprintf(stderr, "can't open %s ... ...\n", file);
      exit(-2);
   }
   
  while(fscanf(ifp, "%d", &n) != EOF)
   { 
      arraySizePtr++;
   }
   
   rewind(ifp);
   
   *arrayPtr = (int *) malloc (*arraySizePtr * sizeof(int));
   if (arrayPtr == NULL)
   {
      fprintf(stderr, "Memory allocation failed... ...\n");
      exit (-3);
   }
   
   for (i=0; i<*arraySizePtr; i++)
   {
      fscanf(ifp, "%d", arrayPtr[i]);   
   }
   fclose(ifp);
}

okay, final check!

also, In main, when i Pass the array to this function, is it correct? judge = CheckLych(array, randomNb, arraySize);

No -- you screwed it up again. Look at your original post, which was correct.

line 22 of the function is wrong too. should be if (*arrayPtr == NULL)

line 30 will not work either. you need to add parantheses, like this: fscanf(ifp, "%d", (*arrayPtr)[i]);

No -- you screwed it up again. Look at your original post, which was correct.

I haven't changed this fucntion the original one is same as

judge = CheckLych(array, randomNb, arraySize);

also i have warning that

fscanf(ifp, "%d", (*arrayPtr));

this says, '%d' expects type 'int *' but argument 3 has type 'int'??

and when i run this
it stops at memory allocation...

>>I haven't changed this fucntion the original one is same as
See line 38 or your original post.

>>this says, '%d' expects type 'int *' but argument 3 has type 'int'??
Oh yes, you need to make it a pointer, like this: fscanf(ifp, "%d", &(*arrayPtr)[i]);

See line 38 or your original post.

Thanx for help! but still can't find my original post. I checked all the post and there is no such line 38 matches
this call

judge = CheckLych(array, randomNb, arraySize);

does it have to like??

judge = CheckLych(&array, randomNb, arraySize);

code:

int CheckLych(int array[], int n, int count)
{
   int counter = 0;
   int i;
   for (i=0; i<count; i++)
   {
      if (n == array[i])
      {
         counter = TRUE;
      }
   }
   return counter;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.