Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign a pointer by a pointer to multidimensional array in function [duplicate]

void print_first_n_row(double **matrix, int n, int row_size) {
 double (*abc)[row_size];
 abc=matrix;}

I am having assignment from incompatible pointer type [-Wincompatible-pointer-types] abc=matrix error. How can I solve this?

like image 317
ajdfhjkshg Avatar asked Jul 31 '26 23:07

ajdfhjkshg


2 Answers

A matrix is an abstract data type. How you implement it is an implementation detail.

In this answer I detailed how you could implement such a type in C.

Multi-dimensional arrays don't really exist in C. C just have arrays of arrays, or arrays of pointers -or other types, including arrays of scalars. Check by reading n1570 (the C11 standard).

I am having assignment from incompatible pointer type [-Wincompatible-pointer-types] abc=matrix error. How can I solve this?

Either think in abstract data types terms, or read more about C dynamic memory allocation (consider using flexible array members) and about the C programming language.

You'll find many examples of matrix-related source code on github or gitlab and elsewhere.

like image 193
Basile Starynkevitch Avatar answered Aug 02 '26 16:08

Basile Starynkevitch


Note: This answer is just regarding the error message, not the logic behind passing pointers to a matrix.


"I am having assignment from incompatible pointer type [-Wincompatible-pointer-types] abc=matrix error. How can I solve this?"

double (*abc)[row_size]; - abc is a pointer to an array of double.

double **matrix - matrix is a pointer to a pointer to double.

There is a mismatch. * vs. **.

You can´t assign the value of a pointer to a pointer to double to a pointer to an array of double.

Change double (*abc)[row_size]; to double **abc or double (**abc)[row_size]; or just use matrix if abc isn´t needed otherwise.

like image 27
RobertS supports Monica Cellio Avatar answered Aug 02 '26 15:08

RobertS supports Monica Cellio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!