2014-02-25 182 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

哦好吧我要去學習如何解決這個問題..謝謝我知道我至少要看! –