1. #1
    Blood_Raven's Avatar
    Registered
    17/07/02
    Location
    U.S.
    Posts
    2,348
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/61

    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  

  2. #2
    Gurdt's Avatar
    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  

  3. #3
    Blood_Raven's Avatar
    Registered
    17/07/02
    Location
    U.S.
    Posts
    2,348
    iTrader
    0
    Mentioned
    0 Post(s)
    Reputation
    0/61
    Hajah zjust. Fixed.
    no votes  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in