I made this function:
char** parse_cmd(const char* cmdline) {
int i;
int j = 0 ,k = 0;
char ch[100][100];
for(i=0; i<strlen(cmdline); i++) {
if(cmdline[i] != ' ') {
ch[j][k] = cmdline[i];
k++;
} else {
j++;
k = 0;
}
}
return ch;
}
But when I compile the program I have this warning:
shell.c: In function ‘parse_cmd’:
shell.c:25:2: warning: return from incompatible pointer type
shell.c:25:2: warning: function returns address of local variable
Why?
You try to return a pointer to a memory location that will not be associated with the array after the function returned. If you want to permanently allocate the memory, then you have to copy it with
malloc(or any similar function) before returning it.e.g:
EDIT: fixed typo. Thanx