Summary of two-dimensional array parameter passing in C Language

Source: Internet
Author: User

Recently, I am working on matrix operations. Based on previous lazy ideas, I want to directly create a function that can transmit a matrix of any dimension for calculation.
 
Therefore, the void matrix_mult (float ** p) function is declared );
 
Then, the main function declares a two-dimensional array float a [3] [3];
 
In this case, matrix_mult ();
 
Then, the compiler prompts: cannot convert parameter 1 from 'float [3] [3] 'to 'float **'
 
There is an error.
 
Correction Method:
 
Matrix_mult (float (* p) [N]); // N declares the second-dimensional size value of the two-dimensional array, and then transmits the parameter matrix_mult (a); OK.
 
 
Summary: this is a common question: array and pointer
 
Note:
 
A pointer is a variable name, while an array is not a variable name. An array is similar to a struct.
 
Example:
 
Int * p; int a [2]; p =;
Int n = sizeof (p );//
 
Int len = sizeof ();
 
The value of n is 4, and the value of len is 8;
 
 
Int a [10] [20]; // a real two-dimensional array,
 
Int * B [10]; // The definition is assigned with 10 pointers without initialization. In other words, B is a pointer array composed of 10 INTEGER (int) pointers.
 
Int (* p) [3]; // pointer to an array
 
Int (* p) (); // pointer to the Function
 
Int * p (); // p is a function, and the returned value is an int pointer.
 
 
Float ** here p is not a pointer to a two-dimensional array, but a pointer to a pointer, that is, a second-level pointer.
The correct pointer to a two-dimensional array should be: Int a [2] [2]; Int (* p) [2]; // when defining the array dimension, only the first dimension can be ignored.
 
 
 
A two-dimensional array is actually a pointer, while a second-level pointer is a pointer to a pointer, so the two are not equivalent.
 
 
 
Appendix: code
 
/*----------------------------------------------------------------------------------*/
 
/* Description: this function can be used to multiply the Matrix 3x3 directly. It has been verified */
 
/*----------------------------------------------------------------------------------*/
 
# Include "stdlib. h"
# Include "stdio. h"
# Include "math. h"
 
 
Void matrix_mult (float (* a) [3], float (* B) [3], float (* c) [3]);
Void matrix_init (float (* a) [3]);
 
 
 
 
Float a [3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Float B [3] [3] = {2, 3, 4, 5, 6, 7, 8, 9, 1 };
Float c [3] [3] = {0 };
 
 
 
Int main ()
{
Int I, j;
 
 
Int t [2];
Int * p;
P = t;
 
 
Int n = sizeof (p );
Int len = sizeof (t );
 
 
Printf ("the original array C value is \ n ");
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Printf ("% f", c [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
 
 
Printf ("the value of original array A is \ n ");
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Printf ("% f", a [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
 
 
Printf ("the original array B value is \ n ");
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Printf ("% f", B [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
 
// Initialize data
Matrix_init (c); // matrix_init (* c) [3]);
 
 
// Matrix_init (* c) [3]);
Printf ("the C value of the initialization array is \ n ");
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Printf ("% f", c [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
 
Matrix_mult (a, B, c); // matrix_mult (float (* a) [3], float (* B) [3], float (* c) [3])
// Matrix_mult (* a) [3], (* B) [3], (* c) [3]);
Printf ("the value of array C after multiplication is \ n ");
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
Printf ("% f", c [I] [j]);
}
Printf ("\ n ");
}
Printf ("\ n ");
 
 
Return 0;
 
}
 
 
Void matrix_mult (float (* a) [3], float (* B) [3], float (* c) [3])
{
Int I, j, k;
 
For (I = 0; I <3; I ++)
{
For (j = 0; j <3; j ++)
{
For (k = 0; k <3; k ++)
{
C [I] [j] + = a [I] [k] * B [k] [j];
}
}
}
 
}
 
 
 
 
Void matrix_init (float (* a) [3])
{
Int I = 3;
Int j = 3; www.2cto.com
 
For (I = 2; I> = 0; I --)
{
For (j = 2; j> = 0; j --)
{
A [I] [j] = 0;
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.