2011-04-23 39 views
1
int sum_s1=0, sum_s2=0; 

int comps(char s1[], char s2[]) { 

    if (s1[0] == '\0' && s2[0] == '\0') { 

    if (sum_s1 == sum_s2) { 
     printf("hihi"); //printable 
     return 1; //return the 4202692 

    } else { 
     return 0; 
    } 

    } else { 

    if (*s1 != '\0') { 

     if (s1[0]!=32) { 
     sum_s1 += s1[0]; 

     } else { 
     sum_s1 += 0; 

     } 
     *s1 = *(s1+1); 
    } 
    if (*s2 != '\0') { 

     if (s2[0]!=32) { 
     sum_s2 += s2[0]; 

     } else { 
     sum_s2 += 0; 

     } 

     *s2 = *(s2+1); 
    } 

     if (*s1 != '\0' && *s2 == '\0') equalIgnoreSpace(s1+1, "\0"); 
     if (*s1 == '\0' && *s2 != '\0') equalIgnoreSpace("\0", s2+1); 
     if (*s1 != '\0' && *s2 != '\0') equalIgnoreSpace(s1+1, s2+1); 
     if (*s1 == '\0' && *s2 == '\0') equalIgnoreSpace("\0", "\0"); 
    } 
} 

int main() { 

    int compa=1; 


    compa = comps("abc f", "abcf"); 
    printf("%d", compa); //return the 4202692 


    if (compa == 1) { 
    printf("Two string are equal"); 
    } else { 
    printf("Two string are not equal"); 
    } 

    getchar(); 
    return 0; 
} 

comps()應該return 1和停止,但我不能在主功能得到1。我怎樣才能解決這個問題?功能無法返回int值

+0

有什麼問題嗎? – littleadv 2011-04-23 05:41:37

+2

請修改以包含問題。 – 2011-04-23 05:42:09

+0

你能提供'equalIgnoreSpace'嗎? – joce 2011-04-23 05:48:49

回答

3

沒有在譜曲函數沒有return語句[在其他情況下]

int comps(char s1[], char s2[]) 
{ 
    if (s1[0] == '\0' && s2[0] == '\0') 
{ 
    if (sum_s1 == sum_s2) 
    { 
     printf("hihi"); //printable 
     return 1; //return the 4202692 
    } 
    else 
     return 0; 
    } 
else 
{ 
    ... 
    ... 
    return code; 
} 
} 
+0

@Sourav的意思是,在你的'int comps()'函數結尾添加一個「dummy」'return 0;'。建議您也可以修復您的'else'邏輯。 – 2011-04-23 05:45:29

0

你試圖修改靜態字符串*s1 = *(s1+1);和你的程序崩潰。試着用這個代替:

int main() { 

    int compa=1; 

    /* Allocated memory can be modified without adverse effects! */ 
    char s1[64]; 
    char s2[64]; 
    strcpy(s1, "abc f"); 
    strcpy(s2, "abcf"); 

    compa = comps(s1, s2); 
    printf("%d", compa); //return the 4202692 

    if (compa == 1) { 
     printf("Two string are equal"); 
    } else { 
     printf("Two string are not equal"); 
    } 

    getchar(); 
    return 0; 
} 

此外,由Sourav提到,comps缺少return語句。編譯它給:

1>c:\code\test\test.cpp(83): warning C4715: 'comps' : not all control paths return a value 

而且compa將分配給它,一旦你爲它分配comps的(不確定)的返回值未定義的值。