開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
GCC
問題(Question):
我有一個B字串跟一個A字串
那我想知道,B字串在A字串中的位置(不管B有沒有出現在A)
所以我使用strstr來處理
int location;
char str_A[20]="Hello world.",str_B[6]="Error";
location=(int)(strstr(str_A,str_B)-str_A);
if(location<0)
return -1;
else
return location;
如果找不到,應該相減後會為負數
但是在Linux下偶爾相減後卻是一個非常大的正數
導致程式判斷就錯誤了,但是Windows卻不會
我是哪邊寫錯或是哪邊需要修改,才能符合我所預期的功能呢?
再麻煩高手幫忙一下,感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.105.253
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1481216662.A.C1A.html
→
12/09 01:40, , 1F
12/09 01:40, 1F
→
12/09 01:40, , 2F
12/09 01:40, 2F
所以負數減掉一個非常大的負數,才會變成正數這樣嗎?
那如果要實現我的功能,我應該怎麼改才好?
※ 編輯: jacky1989 (111.249.105.253), 12/09/2016 01:48:02
→
12/09 02:08, , 3F
12/09 02:08, 3F
→
12/09 02:08, , 4F
12/09 02:08, 4F
→
12/09 02:08, , 5F
12/09 02:08, 5F
→
12/09 02:08, , 6F
12/09 02:08, 6F
→
12/09 02:08, , 7F
12/09 02:08, 7F
推
12/09 02:25, , 8F
12/09 02:25, 8F
→
12/09 02:26, , 9F
12/09 02:26, 9F
→
12/09 02:29, , 10F
12/09 02:29, 10F
→
12/09 02:29, , 11F
12/09 02:29, 11F
→
12/10 23:07, , 12F
12/10 23:07, 12F
推
12/11 00:14, , 13F
12/11 00:14, 13F
→
12/11 00:32, , 14F
12/11 00:32, 14F