2014-10-04 22 views
1

我在PicoC上寫了一個小腳本,爲我的Loxone Miniserver Go獲取公共IP地址。所以我一直都知道我的公有IP。我的計劃是獲得知識產權,將其分成4部分,並將整數設置爲程序大綱。爲什麼strcmp()沒有比較chararray和char?

這裏是腳本

// write program here in PicoC 

char* append(char* addThis, char* toThis) 
{ 
    char* destination = (char*)malloc(strlen(addThis) + strlen(toThis) + 1); 
    strcpy(destination, toThis); 
    strcat(destination, addThis); 
    return destination; 
} 

while(TRUE) 
{ 
    char *result = NULL; 
    result = httpget("checkip.dyndns.org",""); 
    int j = 0; 
    char* p = NULL; 
    p = strstrskip(result,"Current IP Address: "); 
    j=strfind(p, "<", FALSE); 
    char ip[strlen(p)-j]; 
    strncpy(ip,p,j); 
    char *first = malloc(4); 
    char *second = malloc(4); 
    char *third = malloc(4); 
    char *fourth = malloc(4); 
    char *tmp = NULL; 
    for (int i = 0; ip[i] != '\0'; i++) { //Made by me, so it may not be the most efficienet way 
     tmp = malloc(4); 
     if (strcmp(ip[i], ".") || ip[i] != '\0')  //Error 
      tmp = append(tmp, &ip[i]); 
     if (strcmp(ip[i], ".") && first == NULL) {  //Error 
      setlogtext("testing"); 
      setlogtext(tmp); 
      strcpy(frist, tmp); 
      setlogtext(first); 
     } else if (strcmp(ip[i], ".") && second == NULL) { //Error 
      strcpy(second, tmp);  
     } else if (strcmp(ip[i], ".") && third == NULL) { //Error 
      strcpy(third, tmp); 
     } else if (strcmp(ip[i], ".") && fourth == NULL) { //Error 
      strcpy(fourth, tmp);  
     } 
     if (strcmp(ip[i], ".") || ip[i] == '\0') 
      free(tmp); 
    } 
    free(tmp); 
    setlogtext(first); 
    setoutput(0, atoi(first)); 
    setoutput(1, atoi(second)); 
    setoutput(2, atoi(third)); 
    setoutput(3, atoi(fourth)); 
    sleeps(15); 
} 

我也已經讀documentation,但我沒能解決這個問題。

任何人都可以幫助我解決它嗎?

回答

1

我不知道PicoC但我想這裏的問題是,你將不得不C.

的strcmp比較字符串,這只是它的方式是相同的。比較一個字符串和一個字符是沒有意義的:要麼你的字符串是1個字符的長度,在這種情況下,你應該直接比較字符;或者你的字符串不是1個字符長度,在這種情況下,它不會等於字符。

在特定情況下,你應該只比較字符,而不是字符串:

if (ip[i] != '.' || ip[i] != '\0') 
+0

是的,是有道理的。謝謝! – 2014-10-05 20:16:45