Below is the function I copied to get the answer...so plz do tell us the simple method for this.....

int ** addmatrix(int **a,int x1,int y1,int **b,int x2,int y2)
	{
	int i,j,**c;
	c=(int**)malloc(x1*sizeof(int*));
	for(i=0;i<x1;i++)
	*(c+i)=(int*)malloc(y1*sizeof(int));
	if(x1==x2 && y1==y2)
	for(i=0;i<x1;i++)
		for(j=0;j<y1;j++)
		*(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
		return(c);
	}

The simplest way to avoid this, on this forum, is to stay AWAY from "NIGHTS". He's a pointer fan, big time! ;)

Seriously, use indeces (aka indexes), instead of pointers. Let C handle the change from index to pointer value.

Are you trying to add up the whole array?

@Adak

Your right. Pointers make me salivate.

@advait_123

c=(int**)malloc(x1*sizeof(int*));
for(i=0;i<x1;i++)
   *(c+i)=(int*)malloc(y1*sizeof(int));

Highly inefficient. malloc wastes tons of CPU to do its job, so use it as little as possible.

I'd suggest making one big array and performing math to divide it into rows and columns. Its faster than your code.

And by doing it that way you don't need nested for loops, you can just add the arrays in one for loop. That would make the code faster to execute, more efficient, and less lines of code.

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.