I need help returning a 2-d array from a function. I set up the array with no problems, but I want to know how to return it from the function. I was looking at pointer-to-pointer options but I'm confused now.
I want to return mutations in the code above.
Thanks
Code:
char ** setup_mutations()
{
char mutations[256][MAX_STRING];
char ** result;// = malloc(sizeof(char));
int i,j;
char c, current, string[MAX_STRING];
FILE * fp;
//result = malloc(sizeof(char) * 256);
if((fp = fopen("mutations.txt","r"))==NULL){
printf("Mutations file not available\n");
exit(2);
}
for(i=0; i<256; i++){
//result[i] = malloc(sizeof(char));
mutations[i][0] = '\0';
}
while ( (c = fgetc(fp) ) != EOF ){
current = c;
for(c=fgetc(fp); c!=' '&&c!=EOF; c=fgetc(fp)){
;
}
i=0;
for(c=c; c!='\n'&&c!=EOF; c=fgetc(fp)){
string[i] = c;
i++;
}
string[i] = '\n';
i = current;
for(j=0; j!='\n'; j++){
mutations[i][j] = string[j]; // trying to put a string into a char
}
}
if((fclose(fp))==-1)printf("Mutations file didn't close properly\n");
result = mutations;
return result;
};
Thanks
Comment