2014-02-25 138 views
-2

我需要在2D矩阵上工作,但在分配一切后出错了。这基本上是说明。将值赋给2D矩阵

下面的代码:

int matA[1][1], matB[1][7], matRes[1][7]; 
    memset (matA, 0, sizeof (matA)); 
    memset (matB, 0, sizeof (matB)); 
    memset (matRes, 0, sizeof (matRes)); 
     printf("test0 :%d \n",matB[1][0]); 
    //matrice de fonction 
    matB[1][0]= 0; 
     printf("test1 :%d \n",matB[1][0]); 
    matB[1][1]= matB[0][2]= matB[0][3]= 0; 
     printf("test2 :%d \n",matB[1][0]); 
    matB[0][0]= matB[0][4]= matB[0][6]= matB[1][2]= matB[1][4]= matB[1][5]= 1; 
     printf("test3 :%d \n",matB[1][0]); 
    matB[0][1]= matB[0][5]= matB[0][7]= matB[1][3]= matB[1][6]= matB[1][7]= -1; 
     printf("test4 :%d \n",matB[1][0]); 
    //matrice d'entrée 
    matA[0][1]= matA[1][0] = 0; 
     printf("test5 :%d \n",matB[1][0]); 
    matA[0][0]= X; 
     printf("test6 :%d \n",matB[1][0]); 
    matA[1][1]= Y; 
     printf("test7 :%d \n",matB[1][0]); 

和输出:

test0 :0 
test1 :0 
test2 :0 
test3 :0 
test4 :-1 
test5 :-1 
test6 :3 
test7 :3 

注意,X和Y是2个参数的功能。

+1

问题的正确标题。 –

+0

'int matA [1] [1]'是一个1乘1的矩阵。这是你真正想要的吗? –

+0

选项卡初始化为0否? matA = [0] [0] [0] [1] [1] [0] [1] [1] –

回答

7
int matB[1][7]; 
matB[1][0]= 0; 

这不是matB的有效索引。它可能与用于matAmatRes的内存重叠。这就是为什么更改matA更改matB[1][0]

+0

哦好吧我要去学习如何解决这个问题..谢谢我知道我至少要看! –