LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   strcmp not working correctly (https://www.linuxquestions.org/questions/linux-newbie-8/strcmp-not-working-correctly-4175445114/)

micflunu 01-11-2013 06:09 AM

strcmp not working correctly
 
What is the return value of strcmp in c.I wrote the following code in c.
Code:

char parse(char buffer[],int i)
{
 
            buffer[i] = '\0';
            argument[0] = strtok(buffer, " ");
            numberOfArguments = 1;
            argument[numberOfArguments] = strtok(NULL, " ");
            while (argument[numberOfArguments] != NULL) {
            numberOfArguments++;
            argument[numberOfArguments] = strtok(NULL, " ");
                          } 
                               
  return  numberOfArguments;                             
}
changeDirectory(char buffer[],int i){
  parse(buffer,i);   
int l=0;
                                  while(l<numberOfArguments+1){
                                  printf("argument [%d] is %s\n",l,argument[l]);
                                    l++;
}
int main(){
char *s;
while(1)
      {
            printf("myshell>> "); 
            int i = 0;
       
            while ((i < MAX) && ((buffer[i] = getchar()) != '\n') && buffer[i] != EOF)
                i++;
              if (i == MAX) {
                          buffer[MAX-1] = '\0';
                          while (getchar() != '\n');
                          printf("argument too long\n");
                      }
if (!strcmp(argument[0], "cd"))
  changeDirectory(buffer,i);

the out put is always Null after argument[0].That means some how it is loosing the input after "cd".Why is that does strcmp turn the other valus after cd to null???

linosaurusroot 01-11-2013 06:47 AM

It looks as if you are using the argument array (in main()) before populating it (in parse()).


All times are GMT -5. The time now is 10:59 PM.