2014-11-02 78 views
-1

您好任何人都可以看到爲什麼我的程序不打印反向的刺。
我使用了gcc編譯器:gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2C程序在gcc編譯器中跳過此程序中的循環

#include <stdio.h> 
#include <string.h> 

void print_reverse(char *s) { 

    size_t len = strlen(s); 
    char *t = s + len -1; 

    // I believe my Program is skipping this loop don't know why... 
    while (t <= s){ 
     printf("%c", *t); 
     t = t - 1; 
    }  

    puts(t); 
} 


int main() { 

    char *juices[] = { 
     "dragonfrui", "waterberry", "sharonfruit", "uglifruit", 
     "rumberry", "kiwifruit", "mulberry", "strawberry", 
     "blueberry", "blackberry", "starfruit", 

    }; 
    print_reverse(juices[0]); 
    printf("%s\n", juices[0]); 
    return 0; 
} 
+0

變化'而(T <= S)',同時'(T> = S)'! – Blackhat002 2014-11-02 03:20:20

回答

2

你的猜測是正確的。請記住,t從端部到開始移動,所以

while (t <= s){ 

應該是

while (t >= s){ 
+0

哦,好了,現在我明白了。謝謝你的答案。 – inSynergy 2014-11-02 05:31:25

+0

't'不能是一個指針比較指針變得無效,當它變得小於字符串的開始。 – BLUEPIXY 2014-11-02 08:28:20