0

Segmentation fault. I get the error when I try to run code in C. I am not getting errors in similar instances when using pointer.

 #include<stdio.h>
#include<stdlib.h>

int mult(int *w, int u, int v) {
    w[0] = u;
    w[1] = v;
    return 0;
}


int main() {
    int u = 8;
    int v = 2;                
    int *w[2];
    mult(*w, u, v);
    printf("%d%d", w[0], w[1]);
    return 0;
}
Sceng
  • 1
  • 2
    `int *w[2];` -> `int w[2];` and `mult(*w, u, v);` -> `mult(w, u, v);` – Eugene Sh. May 22 '19 at 20:37
  • Please explain your code. What e.g. do you think this does `int *w[2];`? And `mult(*w, u, v);`? In each case explain especially what the `*` before the `w` means. We need to make sure that there is some thought behind your code, because fixing "guessed" code is futile. – Yunnosch May 22 '19 at 21:12

1 Answers1

0

You pass an uninitialized pointer to function mult() and try to assign a value to memory referenced by that pointer. No surprise that you get a segmentation fault. What's the intention of your code ?

Mathias Schmid
  • 431
  • 4
  • 7