0

Execute main, it will ask for input.

Store input in the argbuf.

Then, use strwrd to split argbuf into tokens

Yet, it says "Error: incompatible types in assignment of char * to char[200]"

I cannot figure out why..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char argbuf[200];//used to store input

char *strwrd(char *s, char *buf, size_t len, char *delim){
    s += strcspn(s, delim);
    int n = strcspn(s, delim); /* count the span (spn) of bytes in */
    if (len-1 < n)             /* the complement (c) of *delim */
        n = len-1;
    memcpy(buf, s, n);
    buf[n] = 0;
    s += n;
    return (*s == 0) ? NULL : s;
}



int main(){
    fgets(argbuf, sizeof(argbuf), stdin);
    char token[10][20];
    int index;
    for (index = 0; index < 10; index++) {
        argbuf = strwrd(argbuf, token[index], sizeof(token[index]), " \t");
        if (argbuf == NULL)
            break;
    }
    return 1;
}
mabeiyi
  • 357
  • 2
  • 5
  • 14
  • Thanks for reminding me that. I always try to answer questions...but, i am new to programming... – mabeiyi Sep 17 '12 at 17:48
  • possible duplicate of [Difference between char* and char\[\] in C](http://stackoverflow.com/questions/10054271/difference-between-char-and-char-in-c) – netcoder Sep 17 '12 at 18:02

2 Answers2

5

strwrd returns a char*, so you can't store this value in a char[200] variable. Use char* type instead.

md5
  • 23,373
  • 3
  • 44
  • 93
  • I think "argbuf" represents the address of argbuf[200]? – mabeiyi Sep 17 '12 at 16:57
  • You've declared argbuf as an array, so at times it's appropriate to treat it like a char*. But here, you can't return a char* into argbuf because it's immobile in memory. – desimusxvii Sep 17 '12 at 17:00
  • I declared another pointer "char *buf=NULL". and assign the address of the array to buf "buf=argbuf". Then, use the buf as the parameter of strwrd. Finally, it works. – mabeiyi Sep 17 '12 at 17:45
1

char * and char[x] are different types, see here

Here's another good resource

In your code char argbuf[200]; is a statically allocated array, so you can not assign a pointer to it. Why pass the global back and forth anyway? If you're going to use argbuf as a global variable then just modify it inside strwrd directly if your each for '\t' comes back with something valid.

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177