strcmppicocloxone-miniserver

Why is strcmp() not comparing a chararray and a char?


I am writing a little script in PicoC to get my public IP-address for my Loxone Miniserver Go. So I always know my public IP. My Plan was to get the IP, split it into its 4 parts and set the integers to the programms outout.

Here is the script

// 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);
}

I also already read the documentation but I was not able to fix this issue.

Can anyone help me to fix it?


Solution

  • I don't know PicoC but I guess the problem here is the same you would have in C.

    strcmp compares strings, it's just the way it is. Comparing a string and a char makes no sense: either your string is 1-character length, in which case you should directly compare chars ; or your string is not 1 character length, in which case it won't be equal to the character.

    In your specific case, you should just compare characters, not strings:

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