I tried to perform a training task, it seems I wrote everything correctly, but after entering the array, the program crashes with the error "Ex.exe stopped working". In general, I do not understand what the error is.

#include <stdio.h> #include <stdlib.h> float sum1(int **b1, int m1, int a1) { int i1, j1; float s1=0; for(i1=0; i1<m1; i1++) { for(j1=0; j1<a1; j1++) { if(b1[i1][j1]>0) { s1+=b1[i1][j1]; } } } return s1; } int main() { int i, j, m, a; float sum=0; puts("Vvedite kolichestvo strok pervogo massiva"); scanf("%i", &m); puts("Vvedite kolichestvo stolbcov pervogo massiva"); scanf("%i", &a); int b[m][a]; puts("Vvedite elementi pervogo massiva"); for(i=0; i<m; i++) { for(j=0; j<a; j++) { scanf("%i", &b[m][a]); } } sum=sum1(b, m, a); printf("%f\n", sum); } 
  • What is "b1" in the function sum1? - IronVbif
  • Another mismatch of the type of parameter b in float sum1 (int ** b, int m1, int a1) and sum = sum1 (b, m, a); - alexlz

2 answers 2

The first mistake here (here and fell!)

 scanf("%i", &b[m][a]); 

must be

 scanf("%i", &b[i][j]); 

I also do not really like

 int b[m][a]; 

This, as I recall, will only work normally in gcc, it is better to explicitly allocate memory through malloc.

Fixed how i see it right

 #include <stdio.h> #include <stdlib.h> float sum1(int *b1, int m1, int a1) { int i1, j1; float s1=0; for(i1=0; i1<m1; i1++) { for(j1=0; j1<a1; j1++) { if(*(b1 + i1*a1+j1)>0) { s1+=*(b1 + i1*a1+j1); } } } return s1; } int main() { int i, j, m, a; float sum=0; puts("Vvedite kolichestvo strok pervogo massiva"); scanf("%i", &m); puts("Vvedite kolichestvo stolbcov pervogo massiva"); scanf("%i", &a); int * b = (int*)malloc(a*m*sizeof(int)); puts("Vvedite elementi pervogo massiva"); for(i=0; i<m; i++) { for(j=0; j<a; j++) { scanf("%i", (b + i*a+j)); } } sum=sum1(b, m, a); printf("%f\n", sum); free(b); return 0; } 
  • My experiment with 2 lines, 3 columns and numbers 1 2 3 4 5 6 and the author’s original program resulted in a segmantation fault in line 12 Program received signal SIGSEGV, Segmentation fault. 0x08048497 in sum1 (b1 = 0xbffff2b0, m1 = 2, a1 = 3) at ts.c: 12 .... 12 if (b1 [i1] [j1]> 0) .... (gdb) p b1 [i1 ] [j1] Cannot access memory at address 0x1 (gdb) which is actually expected. - avp
  • But how did you get data input, where scanf("%i", &b[m][a]); ? - KoVadim
  • Indeed, at first I started with scanf ("% i", & b [m] [a]); Accordingly, the matrix was just dirt. But the input went without incident. Fatally the stack did not crash. - avp
  • But it's just lucky. It could well damage the stack to do. The technique has reached the fact that the compilers get arrays a couple of bytes more, which would circumvent (and detect) similar bugs. Of course, there are also bugs further, but even the performance did not reach me :) - KoVadim
  • Or out of luck, it's like looking at things like that. For me, the sooner it collapses, the better. Just by experience, damage to the stack (and the array b [m] [a] is allocated in the stack) puts the program less often than access with an incorrect pointer. - avp

You program in sum1 falls. In fact, type b in it and its call should be described differently.

 float sum1 (int *b, int rows, int cols) { int i; float s = 0; for (i = 0; i < rows*cols; i++) if (b[i] > 0) s += b[i]; return s; } .... sum = sum1(&b[0][0], m, a); .... 

In general, if you want to refer to b [i] [j] in sum1 (), then the formula is

 b[i*cols + j] // i-я строка, j-ый столбец 

You describe sum1 argument b as an array of pointers. Accordingly, the compiler makes code that takes cell b [i1], treats it as a pointer, and is addressed to cell [j1] from that pointer.

In general, much past the place you want.


And what is the user function, I do not understand.

  • one
    @avp: user functions - apparently, not from the library ones, which you should define yourself :-) - VladD