-2

I'm attempting to use a strstr() to find the first occurrence of a double quote ("), however, when I use this line of code:

pch = strstr(tmp,""");

It won't compile because I don't have a terminating quote. So I used

pch = strstr(tmp,'"');

which then tells me I have an error like this:

passing argument 2 of ‘strstr’ makes pointer from integer without a cast [enabled by default]
  pch = strstr(tmp,'"'); //finds the first occurrence and deletes the preceeding
  ^
In file included from /usr/include/stdio.h:29:0,
                 from assignment1.c:1:
/usr/include/string.h:40:8: note: expected ‘const char *’ but argument is of type ‘int’
 char  *_EXFUN(strstr,(const char *, const char *));

Any ideas around this or does anyone know of a way to use strstr to detect a double quote character? Maybe with ASCII conversions?

phuclv
  • 37,963
  • 15
  • 156
  • 475
DonnellyOverflow
  • 3,981
  • 6
  • 27
  • 39
  • 1
    Try this: `strstr(tmp,"\"");` – haccks Nov 09 '14 at 12:48
  • I cannot find that information anywhere. Thank you so much! – DonnellyOverflow Nov 09 '14 at 12:49
  • 2
    This is given in any basic C book/tutorial. For your information look at C11's standard **6.4.4.4 Character constants**: `The single-quote ', the double-quote ", the question-mark ?, the backslash \, and arbitrary integer values are representable according to the following table of escape sequences: single quote' \' double quote" \" question mark? \? backslash\ \\ octal character \octal digits hexadecimal character \x hexadecimal digits` – haccks Nov 09 '14 at 12:54
  • 1
    possible duplicate of [Include double-quote (") in C-string](http://stackoverflow.com/questions/20458489/include-double-quote-in-c-string) – masoud Nov 09 '14 at 12:59

4 Answers4

3

Use pch = strstr(tmp,"\""); and it will work

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

To insert any special characters inside a string you need to escape it by backslash. If the compiler sees the escape character it knows that the following character doesn't have a normal role. \" will notify that the double quote doesn't end the string like normal so "\"" will result in a string with a double quote in it.

The full list of escape sequences is available here

Besides you can search with strstr but in case you just want to find the position of a single character then strchr will be much faster

pch = strchr(tmp,'\"');
phuclv
  • 37,963
  • 15
  • 156
  • 475
1

you have to type \" instead of "

phuclv
  • 37,963
  • 15
  • 156
  • 475
tinyfiledialogs
  • 1,111
  • 9
  • 18
0

You can escape the quotes! like this:

pch = strstr(tmp,"\"");
Rizier123
  • 58,877
  • 16
  • 101
  • 156