#include <stdio.h>
#include <stdlib.h>
char** mlc(char** f){
int count=10;
int size=10;
f=(char**)malloc(count*sizeof(char*));
for(int i=0;i<count;i++){
f[i]=(char*)malloc(size*sizeof(char));
}
return f;
}
int main()
{
char** f;
f=mlc(f);
f[0][0]='1';
f[0][1]='\0';
printf("%s",f[0]);
return 0;
}
I use this code can work perfectly ,But when I use the following code ,It will get segmentation fault:
#include <stdio.h>
#include <stdlib.h>
void mlc(char** f){
int count=10;
int size=10;
f=(char**)malloc(count*sizeof(char*));
for(int i=0;i<count;i++){
f[i]=(char*)malloc(size*sizeof(char));
}
return f;
}
int main()
{
char** f;
mlc(f);
f[0][0]='1';
f[0][1]='\0';
printf("%s",f[0]);
return 0;
}
So ,the main difference is the first code I return the pointer, why the second code get fault?