2013-10-16 34 views
-1
// triangle.h 
struct posn { 
    int x; 
    int y; 
}; 

// struct is_right_triangle(p1, p2, p3) 
struct is_right_triangle(const struct posn *p1, const struct posn *p2, 
const struct posn *p3); 



struct is_right_triangle(const struct posn *p1, const struct posn *p2, 
const struct posn *p3) { 
    const int a = (p1->x - p2->x)*(p1->x - p2->x) + (p1->y - p2->y)*(p1->y - p2->y); 
    const int b = (p1->x - p3->x)*(p1->x - p3->x) + (p1->y - p3->y)*(p1->y - p3->y); 
    const int c = (p3->x - p2->x)*(p3->x - p2->x) + (p3->y - p2->y)*(p3->y - p2->y); 
    if (a + b == c) { 
    return *p1; 
    } else if (b + c == a) { 
    return *p2; 
    } else if (a + c == b) { 
    return *p3; 
    } else { 
    return NULL; 
    } 
} 

爲什麼我得到這個錯誤:得到「錯誤:預期的標識符或‘(’」

./triangle.h:13:26: error: expected identifier or '(' struct is_right_triangle(const 
       struct posn *p1, const struct posn *p2, const struct posn *p3);^
./triangle.h:13:26: error: expected ')' 
./triangle.h:13:25: note: to match this '(' struct is_right_triangle(const struct posn *p1, const struct posn *p2, const struct posn *p3); 
+0

動態初始化一個'const'變量是否在語義上正確? –

回答

4

你沒有指定結構返回的該類型試試這個:

const struct posn *is_right_triangle(const struct posn *p1, const struct posn *p2, const struct posn *p3) {