Thread: Segmentation fault
-
12-07-2010, 21:43 #1
Segmentation fault
Eerste keer dat ik terug in C programmeer na een tijdje, maar welke fout maak ik hier? Ik krijg altijd een segmentation fault.

Code:#include <stdio.h> #include <stdlib.h> void pass(int *array) { array = (int*)malloc(sizeof(int)*3); *(array + 0) = 1; *(array + 1) = 2; *(array + 2) = 3; } int main() { int *array = NULL; unsigned int i = 0; pass(array); for(i=0; i<3; i++) { printf("%d\n", *(array+i)); } free(array); array = NULL; return 0; }Last edited by Blood_Raven; 12-07-2010 at 21:48.
no votes
-
-
12-07-2010, 22:11 #2Approved 9-lifer
- Registered
- 21/08/08
- Location
- Hasselt
- Posts
- 2,653
- iTrader
- 8 (100%)
- Mentioned
- 0 Post(s)
- Reputation
- 5/46
je alloceert "array" in een functie, maar in je main-functie is die nog steeds NULL. Dus ofwel return je die ofwel geef je het adres mee
o/'no votes
-
12-07-2010, 22:21 #3
Hajah zjust. Fixed.
no votes
