I would like some help with pointers: in main function I have initialized variable that should point to the array:
int main() {
int n;
double (*array)[3];
array = fillArray(&n);
The function receives an integer argument, which counts number of rows. The return value of the function should be a pointer to the newly created array, which will be saved to the variable 'array' in main function:
double (*)[3] fillArray(int * n) {
double (*array)[3] = NULL;
int allocated = 0;
*n = 0;
while (1)
{
/*scanning input*/
if (allocated <= *n)
{
allocated += 10;
array = (double(*)[3]) realloc (array, sizeof(*array) * allocated)
}
array[*n][0] = value1;
array[*n][1] = value2;
array[*n][2] = value3;
(*n)++;
}
return array;
}
However, the type of return value isn't right and I am kinda lost. Can anyone tell me what is wrong in this code?
Thank you in advance :)
double (*)[3]is?double (*)[3]is a pointer to an array of 3doubles, which might or might not be the first element of a 2D array ofdoubles. It is the type that a 2D array ofdoublewill decay to if its second dimension is 3. And it is, to be sure, a valid type.