Hi,
I'm trying to make array of struct and I don't get where is my mistake.
can you help me please?
This is the code:
Code:
typedef struct{
int i_index;
int j_index;
int value;
}index_s;
int index_sum(int** arr, int rows, int cols, index_s** ans_arr){
int i;
int j;
int ans = 0;
int arr_i = 0;
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if ((i+j) == arr[i][j]){
ans++;
}
}
}
*ans_arr = (index_s*)malloc(ans*sizeof(index_s));
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
if((i+j) == arr[i][j]){
ans_arr[arr_i]->i_index = i;
ans_arr[arr_i]->j_index = j;
ans_arr[arr_i]->value = arr[i][j];
arr_i++;
}
}
}
return ans;
}
void third_main(){
int** arr;
index_s* ans_arr;
int rows, cols;
int num_of_sums;
printf("Enter number of rows: ");
scanf("%d",&rows);
printf("Enter number of cols: ");
scanf("%d",&cols);
arr = alloc_matrix(rows, cols);
num_of_sums = index_sum(arr, rows, cols, &ans_arr);
printf("i=%d\t j=%d\t value=%d\n", ans_arr->i_index, ans_arr->j_index, ans_arr->value);
}
Thank you