0

So I'm getting this warning in the last line of this block of code I wrote.

int main(int argc, char **argv)   
{
 while(1) {
char buffer[400];
char str;

if(strcmp(argv[1],"start") == 0 )
    { printf("myshell: process has started \n");
            int i=0;
            while (str = strtok(buffer," ") == NULL) {          
            argv[i] = str;  //**This line causes the warning!

Is it because of how I declare str? Is that incorrect? Thank you!

As the title suggests, the warning is warning: assignment makes point from integer without cast.

user3295674
  • 893
  • 5
  • 19
  • 42
  • Post the error please. – maelswarm Feb 19 '14 at 02:37
  • You're assigning a `char` to a `char*`. And that shouldn't be the only warning/error you get in this. The previous line is just as bad, and doesn't do what you think it does. You're missing a set of parens. – WhozCraig Feb 19 '14 at 02:38
  • @WhozCraig How can I fix this? I tried making char str into char *str but that's not it. Also, what makes you say about the more errors? I'm compiling my code and it's the only one I'm getting, should I be concerned? – user3295674 Feb 19 '14 at 02:41
  • @user3295674 just because it compiles doesn't mean it is correct. Ex: the condition in your while-clause. It is assigning the expression `strtok(buffer, " ") == NULL` to a `char` variable. It is *not* assigning `strtok(buffer, " ")` to a `char*`, then testing for `NULL`. – WhozCraig Feb 19 '14 at 02:50

2 Answers2

0

The prototype of strtok() is char *strtok(char *str, const char *delim);, the return value of strtok() is char *, therefore the type of str should be char *.

What is more, str = strtok(buffer," ") == NULL means str = (strtok(buffer," ") == NULL), probably not what you want, I guess you mean (str = strtok(buffer," ")) == NULL.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

I'm not sure how this could result in a cast error but I do see an error in your code. The first call to strtok initializes the token'izer since you pass it a buffer. The second call, assuming you want it to find the next token, should have a null value instead of the same buffer.

Look at this example...

So what you want is:

...
int i=0;
str = strtok(buffer, " ");
do {          
    argv[i] = str;  //**This line causes the warning!
    ...
} while (str = strtok(NULL, " ") != NULL);
John Yost
  • 693
  • 5
  • 20
  • ...you also need to fix the "char str;" to "char* str" as WhozCriag mentions. – John Yost Feb 19 '14 at 02:58
  • Would it need extra parenthesis around (str = strok(NULL, " ")) in the last line? Also, would this also work? int i=0; str = strtok(buffer, " "); while (str == NULL) { argv[i] = str; i = i+1; } – user3295674 Feb 19 '14 at 03:04
  • Yes and no. "while (str == NULL) {..." would never enter the loop assuming the buffer had at least one space. The first call to strtok returns a pointer to the first string, not null. So I think you mean "while (str != NULL) {..." – John Yost Feb 19 '14 at 03:06