I programmed some functions that use a dynamic array of

double ** array

.

But know, I want to test the results with a small array of 5 x 5 elements and use this array in the functions I coded.

My problem is that I can find no way to have a new ** use the 5 x 5 array.

Previously, I had similarities built like this:

void getSimilarities(const int sz,  double **ars);

double * similarities = (double **) malloc(sample_size * sizeof(double *));
if (! similarities) {
    cout << "Not enough memory!" << endl;
		exit(1);
	}

    for(unsigned int i = 0; i < sample_size; i++) {
		similarities[i] = (double *) malloc(sample_size * sizeof(double));
		if(! similarities[i]) {
			printf("Not enough memory.\n");
			exit(1);
		}
	}



getSimilarities(sample_size, similarities);

And now, I'd like to try the similarities_b array:

double similarities_b[5][5] = { 
			{1.0,	0.0,	1.0,	1.0,	1.0},
			{0.0,	1.0,	1.0,	0.0,	0.0},
		        {1.0,	1.0,	1.0,	0.0,	1.0},
			{1.0,	0.0,	0.0,	1.0,	1.0},
			{1.0,	0.0,	1.0,	1.0,	1.0}
	};

double ** similarities = similarities_b;

But gcc gives this error:

error: cannot convert 'double (*)[5]' to 'double**' in initialization

I'm new to C, and don't know how to solve this.

you need to assign every element one by one
try something like this:

//initialize dynamic array
double ** similarities;
int i, j;
      similarities = (double **) malloc (5 * sizeof(double*));
      //then check if it can't allocate enough memory
          for( i = 0; i<5 ; i++){
              *(similarities + i) = (double *) malloc (5 * sizeof(double));
              //*(similarities + i) can be similarities[i]
          }
for (i = 0; i<5; i++){
     for (j = 0; j<5; j++){
     similarities [i][j] = similarities_b [i][j]; //assign values
     //similarities[i][j] can be *(*(similarities + i) + j)
     }
}

Thank you very much!

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.