We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,752 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to have double ** use given array of [5][5]?

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.

2
Contributors
2
Replies
5 Days
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
luislupe
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)
     }
}
zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13

Thank you very much!

luislupe
Newbie Poster
14 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by zeroliken

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1149 seconds using 2.7MB