0

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.

Zandees
  • 1
  • 1
  • 1
    `int size = sizeof(array);` does not do what you think it does. you also need to post a [mcve] – OldProgrammer Nov 27 '22 at 14:39
  • Well i used it before when it worked because i needed to know how many values will be stored and it worked just how i wanted it to – Zandees Nov 27 '22 at 14:43
  • 2
    If the function has access to the _array_ [and _not_ just a _pointer_ to an array--as you have here], you can get the maximum size with (e.g.) `sizeof(arr) / sizeof(arr[0])` as `sizeof` is expressed in units of `char`. But, you have a pointer, so `sizeof` returns 4 on a 32 bit machine or 8 on a 64 bit machine. In C, an array does not have a count attached to it. You have to keep track of that yourself. So, pass the count as an argument to the function. – Craig Estey Nov 27 '22 at 14:48
  • At "I used it before because... and it worked how i wanted it to" it happened to work out by accident. – Ahmed Masud Nov 27 '22 at 15:03
  • Well i a have a variable in which i have stored number of lines in my char array but that works only if i insert 1 line of string. If there is more then 1 line on stdin then there is the core dumped error again – Zandees Nov 27 '22 at 15:08
  • Yes it does, thanks. However even with the right size of my array the core dumped error still ocurres. For size of array 1 everything is fine, but when its 2 then the second pair of values is 0 and i end up with core dumped error. – Zandees Nov 27 '22 at 15:54

1 Answers1

0

So i've made few changes in the function and everything works fine now.

void  ExtractNumber (char ** array, double ** Numbers1, double ** Numbers2, int  ArrMax)
{

double a,b;
int size = ArrMax;
char*  str;

double * tmp1;
double * tmp2;

tmp1 = (double*) malloc (sizeof (tmp1) * size);
tmp2 = (double*) malloc (sizeof (tmp2) * size);

for (int i = 0; i < size; i++)
{
str = array[i];
sscanf(str, "%lf,%lf",&a, &b);
tmp1[i] = a;
tmp2[i] = b;
}
free(*Numbers1);
free(*Numbers2);

*Numbers1 = tmp1;
*Numbers2 = tmp2;
}

I made a variable which keeps the number of lines stored in charr array which i later used in the for cycle. I also just made two temporary arrays which i then copied into my original arrays.

Zandees
  • 1
  • 1