Question:I have a function that takes two 2d matrices of variable dimensions and returns a matrix of equal dimensions.
double(*)is implicitly promoting the data type. Is there a way that I can return
Answer:This code is valid.
The return type of the function is
double (*), i.e. a pointer to an array of unspecified size of type
returnstatement in this function is converting a
double (*)[columns]to that type, and the assignment of the return value in the calling function performs the opposite conversion.
Two pointer types are compatible if they point to compatible types. So now we compare the types
double . Two arrays are compatible if the element types are compatible and, if both specify a size, that the sizes are the same. Since only one specifies a size, the array types are compatible, and therefore so are the pointers to the arrays.
Sections 184.108.40.206p2 and 220.127.116.11p6 of the C standard spell out this compatibility in more detail:
18.104.22.168p2 For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types.
22.214.171.124p6 For two array types to be compatible, both shall have compatible element types, and if both size specifiers are present, and are integer constant expressions, then both size specifiers shall have the same constant value. If the two array types are used in a context which requires them to be compatible, it is undefined behavior if the two size specifiers evaluate to unequal values.
So let’s say
nis 5. The function then creates a pointer to an array whose type is
double (*). This type is converted to
double (*)when it is returned from the function, then converted back to
double (*)when assigned to
sumResultwhose type matches the original type.
So both conversions are valid, and use of
If you have better answer, please add a comment about this, thank you!