I have function ExtractNumber, which i call from main function like this:
ExtractNumber(Array, &Numbers1, &Numbers2);
char** Array is dynamically allocated array containing strings from stdin which start with double,double and then some string. Arrays doubleNumbers1 and double Numbers2 are dynamically allocated arrays into which i want to extract values from previous char array.
ExtractNumbers function looks like this:
//char ** array is my dynamically allocated array
//which reads string from stdin until EOF signal.
//double **Numbers1 and double **Numbers2 are my arrays for those
//double values, which are allocated in main function
void ExtractNumber (char **array, double **Numbers1, double **Numbers2)
{
double a,b;
char* str;
//ArrMax is variable where number of lines stored in
for (int i = 0; i < *ArrMax; i++)
{
str = array[i];
printf ("\n%s\n", str);
sscanf(str, "%lf,%lf",&a, &b);
*Numbers1[i] = a;
*Numbers2[i] = b;
printf ("%lf %lf",(*Numbers1)[i] , (*Numbers2)[i]);
}
}
//main function looks like this
//LenMax calculates the number of lines in Array
int main ()
{
char** Array;
int LenMax;
double * Numbers1;
double * Numbers2;
printf ("Insert string:\n");
Array = ReadArray(&LenMax);
Numbers1 = (double*) malloc (sizeof (Numbers1) * LenMax);
Numbers2 = (double*) malloc (sizeof (Numbers2) * LenMax);
ExtractNumber(Array, &Numbers1, &Numbers2);
for (int i = 0; i < LenMax; i++)
printf ("%lf\n", Numbers1[i]);
for (int i = 0; i<LenMax;i++)
{
free (Array[i]);
}
return 0;
}
I put those printfs there just to test if there is problem with str or *Numbers1 inside the function, there arent and everything is printed just as i want to but i still get the core dumped error and when i try to printf the values from outside the function it says (null). Any suggestions ?
I had it working before but after that i made some changes and it stopped working. Im pretty sure that now its almost identical to how it was before but still cant find whats wrong. I have similar function in other program and there it works just fine and im super confused rn. I tried to make two temporary arrays inside the function to which i save the values and then transfer them to those Numbers1 and Numbers2 values but it also didnt work.
EDIT: I changed the code to this
void ExtractNumber (char ** array, double ** Num1, double ** Num2, int ArrMax)
{
double a,b;
char* str;
//ArrMax is variable in which the number of lines in my array is stored
for (int i = 0; i < ArrMax; i++)
{
str = array[i];
sscanf(str, "%lf,%lf",&a, &b);
*Num1[i] = a;
*Num2[i] = b;
}
}
The core dumped error doesnt occure anymore However if there are 2 lines on stdin, the function is able to read only the values from the first one and the values from the second one are zeros.