2014-06-22 50 views
-1

我如何分割和乘以C中的2個變量與指針? 我嘗試這樣做:指針分區可能嗎?

int multiplicate(int *x,int *y) 
{ 
*x**y; 
} 


int divide(int *x,int *y) 
{ 
*x/*y; 
} 
+0

跑步以後你得到了什麼? – haccks

+3

使用parens !!! '(* X)*(* Y)'!!!! – Michael

+1

你應該在某處存儲結果。 – PMF

回答

2

你缺少return聲明:

int multiplicate(int* x, int* y) 
{ 
    return (*x) * (*y); 
} 


int divide(int *x,int *y) 
{ 
    return (*x)/(*y); 
} 
+0

括號在這裏是不必要的,因爲取消引用'*' - 操作符綁定更緊密,然後乘法'*'操作符。 – alk

2

使用*x/(*y)代替。否則它被解釋爲多行評論。並且忘記了return

2

由於間接運算符的優先級高於乘法運算符,所以不需要括號。

簡單地增加一些空白會做:

int multiplicate(int *x,int *y) 
{ 
    return *x * *y; 
} 


int divide(int *x,int *y) 
{ 
    return *x/*y; 
} 
0

指針司可能嗎?

你不要試圖在這裏devide指針,但使用反引用* - 運算符,指針指向的值是使用。

然而,正如所有的例子給出到目前爲止完全錯過重要的錯誤檢查,我提出以下方法:

int multiply(int * pr, int * px, int * py) 
{ 
    int result = 0; /* Be optimistic. */ 

    if (NULL == pr || NULL == px || NULL == py) 
    { 
    errno = EINVAL; 
    result = -1; 
    } 
    else 
    { 
    *pr = *px * *py; 
    } 

    return result; 
} 

int devide(int * pr, int * px, int * py) 
{ 
    int result = 0; /* Be optimistic. */ 

    if (NULL == pr || NULL == px || NULL == py) 
    { 
    errno = EINVAL; 
    result = -1; 
    } 
    elseif (0 = *py) /* Division by zero is not defined. */ 
    { 
    errno = EDOM; 
    result = -1; 
    }  
    else 
    { 
    *pr = *px/*py; 
    } 

    return result; 
} 

調用這些函數是這樣的:

#include <stdio.h> 
#include <errno.h> 

/* The two functions above go here. */ 

int main(int argc, char ** argv) 
{ 
    int result = EXIT_SUCCESS; 

    if (2 > argc) 
    { 
    fprintf("Missing or invalid arguments.\n"); 
    printf("Usage: %s x y\n", argv[0]); 
    result = EXIT_FAILURE; 
    } 

    if (EXIT_SUCCESS == result) 
    { 
    int x = atoi(argv[1]); /* Better use strtol here. */ 
    int y = atoi(argv[2]); /* Better use strtol here. */ 

    printf("Got x = %d and y = %d.\n", x, y); 

    { 
     int r; 
     if (-1 == multiply(&r, &x, &y) 
     { 
     perror("multiply() failed"); 
     } 
     else 
     { 
     printf("%d * %d = %d\n", x, y, r); 
     } 
    } 

    { 
     int r; 
     if (-1 == multiply(&r, &x, &y) 
     { 
     perror("devide() failed"); 
     } 
     else 
     { 
     printf("%d/%d = %d\n", x, y, r); 
     } 
    } 
    } 

    return result; 
} 

最後更理智的方式將在需要的地方使用指針只有

一個可能的計算策略修改上面的例子是這樣的:

int multiply(int * pr, int x, int y) 
{ 
    int result = 0; /* Be optimistic. */ 

    if (NULL == pr) 
    { 
    errno = EINVAL; 
    result = -1; 
    } 
    else 
    { 
    *pr = x * y; 
    } 

    return result; 
} 

int devide(int * pr, int x, int y) 
{ 
    int result = 0; /* Be optimistic. */ 

    if (NULL == pr) 
    { 
    errno = EINVAL; 
    result = -1; 
    } 
    elseif (0 = y) /* Division by zero is not defined. */ 
    { 
    errno = EDOM; 
    result = -1; 
    }  
    else 
    { 
    *pr = x/y; 
    } 

    return result; 
} 

這樣調用這些函數:

#include <stdio.h> 
#include <errno.h> 

/* The two functions above go here. */ 

int main(int argc, char ** argv) 
{ 
    int result = EXIT_SUCCESS; 

    if (2 > argc) 
    { 
    fprintf("Missing or invalid arguments.\n"); 
    printf("Usage: %s x y\n", argv[0]); 
    result = EXIT_FAILURE; 
    } 

    if (EXIT_SUCCESS == result) 
    { 
    int x = atoi(argv[1]); /* Better use strtol here. */ 
    int y = atoi(argv[2]); /* Better use strtol here. */ 

    printf("Got x = %d and y = %d.\n", x, y); 

    { 
     int r; 
     if (-1 == multiply(&r, x, y) 
     { 
     perror("multiply() failed"); 
     } 
     else 
     { 
     printf("%d * %d = %d\n", x, y, r); 
     } 
    } 

    { 
     int r; 
     if (-1 == multiply(&r, x, y) 
     { 
     perror("devide() failed"); 
     } 
     else 
     { 
     printf("%d/%d = %d\n", x, y, r); 
     } 
    } 
    } 

    return result; 
}