私はCの新機能ですが、なぜこのエラーが出るのかわかりません。私はセグメンテーションフォールトが私の範囲外に行くことが原因であることを知っていますが、私はどこにいるのかわかりません。 私のコードでセグメンテーションフォールト
#include <stdlib.h>
#include <stdio.h>
int** totalMatrix(int numRows, int numCols){
int** firstMatrix;
int** secondMatrix;
int** sumMatrix;
int row, col;
printf("Enter Matrix A\n");
firstMatrix = (int**)malloc(numRows * sizeof(int*));
for(row = 0; row < numRows; row++){
firstMatrix[row] = (int*)malloc(numCols * sizeof(int));
}
for(row = 0; row < numRows; row++){
for(col = 0; col < numCols; col++){
scanf("%d", &firstMatrix[row][col]);
}
}
printf("Enter Matrix B\n");
secondMatrix = (int**)malloc(numRows * sizeof(int*));
for(row = 0; row < numRows; row++){
secondMatrix[row] = (int*)malloc(numCols * sizeof(int));
}
for(row = 0; row < numRows; row++){
for(col = 0; col < numCols; col++){
scanf("%d", &secondMatrix[row][col]);
}
}
printf("A + B =\n");
sumMatrix = (int**)malloc(numRows * sizeof(int*));
for(row = 0; row < numRows; ++row){
for(col = 0; col < numCols; ++col){
sumMatrix[row][col] = firstMatrix[row][col] + secondMatrix[row][col];
printf("%d ", sumMatrix[row][col]);
}
printf("\n");
}
return 0;
}
void delete_matrix(int numRows, int** matrix){
int row;
for(row = 0 ; row < numRows; ++row){
free(matrix[row]);
}
free(matrix);
}
int main(){
int numRows, numCols;
int** matrix;
printf("Please Enter the number of rows: ");
scanf("%d", &numRows);
printf("Please Enter the number of cols: ");
scanf("%d", &numCols);
matrix = totalMatrix(numRows, numCols);
delete_matrix(numRows, matrix);
return 0;
}
事前に感謝
。
ようこそStackOverflow!関連するコードを質問に貼り付ける必要があります。また、今は無駄に思えるかもしれませんが、実際にはデバッガの使い方を学ぶのに最適なタイミングです。 – AndyG
また、関連性のない言語にタグを付けないでください。 –
ペーストビンのリンクです。 – Motosuwa