user-0 提问: C语言指针访问二维数组问题

回复:
user: user-1
content: 仅供参考：//C++ Operators //  Operators specify an evaluation to be performed on one of the following: //    One operand (unary operator) //    Two operands (binary operator) //    Three operands (ternary operator) //  The C++ language includes all C operators and adds several new operators. //  Table 1.1 lists the operators available in Microsoft C++. //  Operators follow a strict precedence which defines the evaluation order of //expressions containing these operators.  Operators associate with either the //expression on their left or the expression on their right;    this is called //“associativity.” Operators in the same group have equal precedence and are //evaluated left to right in an expression unless explicitly forced by a pair of //parentheses, ( ). //  Table 1.1 shows the precedence and associativity of C++ operators //  (from highest to lowest precedence). // //Table 1.1   C++ Operator Precedence and Associativity // The highest precedence level is at the top of the table. //+------------------+-----------------------------------------+---------------+ //| Operator         | Name or Meaning                         | Associativity | //+------------------+-----------------------------------------+---------------+ //| ::               | Scope resolution                        | None          | //| ::               | Global                                  | None          | //| [ ]              | Array subscript                         | Left to right | //| ( )              | Function call                           | Left to right | //| ( )              | Conversion                              | None          | //| .                | Member selection (object)               | Left to right | //| ->               | Member selection (pointer)              | Left to right | //| ++               | Postfix increment                       | None          | //| --               | Postfix decrement                       | None          | //| new              | Allocate object                         | None          | //| delete           | Deallocate object                       | None          | //| delete[ ]        | Deallocate object                       | None          | //| ++               | Prefix increment                        | None          | //| --               | Prefix decrement                        | None          | //| *                | Dereference                             | None          | //| &                | Address-of                              | None          | //| +                | Unary plus                              | None          | //| -                | Arithmetic negation (unary)             | None          | //| !                | Logical NOT                             | None          | //| ~                | Bitwise complement                      | None          | //| sizeof           | Size of object                          | None          | //| sizeof ( )       | Size of type                            | None          | //| typeid( )        | type name                               | None          | //| (type)           | Type cast (conversion)                  | Right to left | //| const_cast       | Type cast (conversion)                  | None          | //| dynamic_cast     | Type cast (conversion)                  | None          | //| reinterpret_cast | Type cast (conversion)                  | None          | //| static_cast      | Type cast (conversion)                  | None          | //| .*               | Apply pointer to class member (objects) | Left to right | //| ->*              | Dereference pointer to class member     | Left to right | //| *                | Multiplication                          | Left to right | //| /                | Division                                | Left to right | //| %                | Remainder (modulus)                     | Left to right | //| +                | Addition                                | Left to right | //| -                | Subtraction                             | Left to right | //| <<               | Left shift                              | Left to right | //| >>               | Right shift                             | Left to right | //| <                | Less than                               | Left to right | //| >                | Greater than                            | Left to right | //| <=               | Less than or equal to                   | Left to right | //| >=               | Greater than or equal to                | Left to right | //| ==               | Equality                                | Left to right | //| !=               | Inequality                              | Left to right | //| &                | Bitwise AND                             | Left to right | //| ^                | Bitwise exclusive OR                    | Left to right | //| |                | Bitwise OR                              | Left to right | //| &&               | Logical AND                             | Left to right | //| ||               | Logical OR                              | Left to right | //| e1?e2:e3         | Conditional                             | Right to left | //| =                | Assignment                              | Right to left | //| *=               | Multiplication assignment               | Right to left | //| /=               | Division assignment                     | Right to left | //| %=               | Modulus assignment                      | Right to left | //| +=               | Addition assignment                     | Right to left | //| -=               | Subtraction assignment                  | Right to left | //| <<=              | Left-shift assignment                   | Right to left | //| >>=              | Right-shift assignment                  | Right to left | //| &=               | Bitwise AND assignment                  | Right to left | //| |=               | Bitwise inclusive OR assignment         | Right to left | //| ^=               | Bitwise exclusive OR assignment         | Right to left | //| ,                | Comma                                   | Left to right | //+------------------+-----------------------------------------+---------------+

user: user-2
content: TranspositionMatrix函数有两处问题： 1.内层循环j应该从0循环到COL 2.[]的优先级比*要高，所以要加上括号，而且括号内写的也不对，改成以下两种方式都可以 (*iTMartix)[j*ROW+i]=(*iSMartix)[i*COL+j]; 或 iTMartix[j][i]=iSMartix[i][j];  #include<stdio.h> #define ROW 3 #define COL 4  void InputMatrix(int iMatrix[ROW][COL]); void DisplaypSource(int iMatrix[][COL], int iROW); void TranspositionMatrix(int (*iSMartix)[COL], int (*iTMartix)[ROW]); void DisplayTarget(int iMatrix[][ROW], int iCOL); void prog9_7();  void prog9_7()  {  	int iSourceMatrix[ROW][COL];		//用于存放原始矩阵数据  	int iTargetMatrix[COL][ROW];		//用于存放结果矩阵数据    	InputMatrix(iSourceMatrix);  	DisplaypSource(iSourceMatrix, ROW);  	TranspositionMatrix(iSourceMatrix, iTargetMatrix);  	DisplayTarget(iTargetMatrix, COL);  }  void main()  {  	prog9_7();  }    //输入数据  void InputMatrix(int iMatrix[ROW][COL])  {  	int i,j;    	printf("请输入数据：\n");  	for (i = 0; i < ROW; i++)  		for (j = 0; j < COL; j++)  		{  			scanf("%d", &iMatrix[i][j]);  		}  		printf("\n");  }    //输出数据  void DisplaypSource(int iMatrix[][COL], int iROW)  {  	int i,j;    	printf("输出数据：\n");  	for (i = 0; i < ROW; i++)  	{  		for (j = 0; j < COL; j++)  		{  			printf("%5d", iMatrix[i][j]);  		}  		printf("\n");  	}  }    void DisplayTarget(int iMatrix[][ROW], int iCOL)  {  	int i,j;    	printf("输出数据：\n");  	for (i = 0; i < COL; i++)  	{  		for (j = 0; j < ROW; j++)  		{  			printf("%5d", iMatrix[i][j]);  		}  		printf("\n");  	}  }    //矩阵转置  void TranspositionMatrix(int (*iSMartix)[COL], int (*iTMartix)[ROW])  {  	int i,j;    	printf("矩阵转置中......\n\n");  	for (i = 0; i < ROW; i++)  	{  		for (j = 0; j < COL; j++)  		{ 			(*iTMartix)[j*ROW+i]=(*iSMartix)[i*COL+j]; 			//iTMartix[j][i]=iSMartix[i][j];  		}  	}  	printf("转置完成\n\n ");  }