dimanche 29 mars 2015

Reading strings doesn't work

I have this little code and I really can't figure out why it always gives me SIGSEGVs.



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

int read_strs(char **a, int *len) {

int i;
scanf("%d", len);
if(*len <= 0) return 1;
a = (char **) malloc(sizeof(char*) * (*len));
if(a == NULL) return 1;
for( i = 0; i < *len; i++ )
{
a[i] = malloc(sizeof(char) * 1000);
scanf("%s", a[i]);
}

return 0;

}


int main(void) {
int i, n;
char **array;

read_strs(array, &n);
for( i = 0; i < n; i++ )
printf("%s\n", array[i]);
return 0;
}


It seems it doesn't even alloc the memory.


Aucun commentaire:

Enregistrer un commentaire