How can we store the return value of "strstr()" in an "int" variable in C Language ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can we store the return value of "strstr()" in an "int" variable in C Language ?

As per the C language, the strstr function returns a "char*". So how is the following code running fine and printing "true" ? [ I got this question in a challenge of Me Vs SoloLearn ] Help me out guys !!! CODE : #include<stdio.h> #include <string.h> void main() { char * str1 = "Hello World"; char * str2 = "rl"; int a = strstr(str1,str2); if(a!='\0') printf("true"); else printf("false"); }

25th Jun 2019, 8:37 AM
cool_boy12344
cool_boy12344 - avatar
2 Answers
+ 4
you can't because the return value is not an int but a pointer to the first occurrence of str2, if str2 cannot be found the return value is NULL, so you can work your way around that by using if(strstr(str1, str2)!=NULL)
25th Jun 2019, 9:17 AM
✳AsterisK✳
✳AsterisK✳ - avatar
0
We can use char* , I run the above program , It shows no ERROR Am ,I Write.....
2nd Jul 2019, 4:04 AM
Ashwin S
Ashwin S - avatar