3
char *local_buffer, *buff;
fgets(buff, 1024, fp);

local_buffer=strtok(buff,'\t'); //Error is coming with this line

I have already tried passing a character variable instead of '\t', but still its showing the same error.

Mat
  • 202,337
  • 40
  • 393
  • 406
user2812534
  • 31
  • 1
  • 1
  • 2

2 Answers2

6

You're passing in a character constant (which is equivalent to an integer), not a string, for the second argument.

local_buffer=strtok(buff,'\t');

What you want instead is:

local_buffer=strtok(buff,"\t");
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • Yup. As mentioned [here](http://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c) . Single quotes specifies a single char, which is interpreted as an int. ASCII code of that symbol for sure. – Kamiccolo Sep 24 '13 at 19:29
  • 1
    @Kamiccolo `'\t'` does not specify ASCII. Would work just as well with any other character C compliant encoding. Certainly ASCII is the most common. – chux - Reinstate Monica Sep 24 '13 at 19:51
  • @chux thanks :) I doubt any of C standart deals with char encoding. I think compiler does. For example, as I remember, GCC has some flags for that. ummmm.... Some discousions about that: [default encoding for C strings](http://stackoverflow.com/q/3996026/1150918), [encoding of C constants](http://stackoverflow.com/q/14384601/1150918), [gcc flags for charset control](http://stackoverflow.com/a/12217048/1150918)... – Kamiccolo Sep 24 '13 at 20:42
3

Try:

char *local_buffer, buff[1024];
fgets(buff, 1024, fp);

local_buffer=strtok(buff,"\t"); //Error is coming with this line

Explanation:

Double quotes ("") around characters represent a null-terminated C-style character string (char*)

Single quotes ('') around a character represents a character (apparently int)

George Mitchell
  • 1,158
  • 2
  • 11
  • 24
  • 1
    A quibble: Though string literals are read-only, they are not `const`. A string literal is of type `char[N]`, where `N` is the length of the string plus one. It's converted, in most contexts, to `char*`. – Keith Thompson Sep 24 '13 at 19:35
  • Suggest `char buff[1024]` instead of `char *buff`. – chux - Reinstate Monica Sep 24 '13 at 19:52
  • @KeithThompson: Hmm... I've never caught that before so I just skimmed the C99 docs... So technically a string literal is not a constant expression, but they are "evaluated" at translation time and modification of a pointer to a string literal is not caught by the compiler but is UB... Interesting... – George Mitchell Sep 24 '13 at 20:03
  • @Chux: Good catch, I saw the one mistake and was like: "Oooh! Oooh! I found it!" ... I blame it on cacheing and the evil that is "copy-and-paste". Thanks! – George Mitchell Sep 24 '13 at 20:05
  • @GeorgeMitchell: Right. It's for backward compatibility. Pre-ANSI C didn't have `const`, so there was no way to write a function that took a `char*` and promised not to modify what its parameter points to. A function that can take a string literal as an argument: `foo("hello")` *should* declare its parameter as `const char*`, but mandating it (by making string literals `const`) would have broken pre-ANSI code that didn't have the option of using `const`. (In C++, string literals are `const` because there was less concern for backward compatibility.) – Keith Thompson Sep 24 '13 at 20:10
  • @GeorgeMitchell: BTW, it's not modification of a pointer that's the issue, it's modification of what it points to. – Keith Thompson Sep 24 '13 at 20:10