All pastes #388777 Raw Edit

Anonymous

public c v1 · immutable
·published 2007-03-10 08:35 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>void someFunction(char ** array, int * arraysize);int main(int argc, char * argv[]){        int arraysize;        char ** somearray;        someFunction(somearray, 5);        printf("%s\n", somearray[0]);           // Results in SEGFAULT ... why?        return 0;}void someFunction(char ** array, int arraysize){        int somebuflen = 20;        //arraysize = malloc(sizeof(int));        //(*arraysize) = 5;                       // We determine size here        array = malloc(arraysize*sizeof(*array)); // sizeof(*array) is the same as sizeof(char **)) in this particular case        int i;        for (i = 0; i < arraysize; ++i)        {                array[i] = (char *)malloc(somebuflen); // malloc will allocate the amount of bytes, i believe C defines a char as the size of 1 byte        }        array[0] = "dummy";        printf("%s\n", array[0]);               // prints within someFunction}