Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. C string problem
  2. char** parse_cmd(const char* cmdline) {
  3.     int i;
  4.     int j = 0 ,k = 0;
  5.     char ch[100][100];
  6.     for(i=0; i<strlen(cmdline); i++) {
  7.         if(cmdline[i] != ' ') {
  8.             ch[j][k] = cmdline[i];
  9.             k++;
  10.         } else {
  11.             j++;
  12.             k = 0;
  13.         }
  14.     }
  15.  
  16.     return ch;
  17. }
  18.        
  19. shell.c: In function ‘parse_cmd’:
  20. shell.c:25:2: warning: return from incompatible pointer type
  21. shell.c:25:2: warning: function returns address of local variable
  22.        
  23. char** parse_cmd(const char* cmdline) {
  24. int i;
  25. int j = 0 ,k = 0;
  26. char **ch = (char**)malloc(100*100);
  27. for(i=0; i<strlen(cmdline); i++) {
  28.     if(cmdline[i] != ' ') {
  29.         ch[j][k] = cmdline[i];
  30.         k++;
  31.     } else {
  32.         j++;
  33.         k = 0;
  34.     }
  35. }
  36. return ch;
  37. }