使用指针

时间:2016-03-28 18:12:48

标签: c arrays pointers

所以我的程序应该计算一个带有不规则五边形十字架的倾斜圆柱体的面积。我已经设法通过使用循环单独传递值来获得更多代码,但我想缩短我的程序并使其更有效。根据我的理解,我可以将变量的地址传递给函数并将其存储在指针中,然后我可以将该指针的值设置为数组中的第一个值,并通过调用main函数中的变量来访问它。然后我明白通过使用类似*(p + 1)的东西,我可以访问数组中的下一个值。但是,我似乎无法弄清楚这是如何工作的。这是我的代码。我的程序通过读取一组5个坐标,计算线条的长度,找到它们形成的三角形区域,将它们相加并乘以形状的高度来工作。任何帮助都是相关的。 顺便说一下:程序运行时,* p和line [0]的值是相同的,但其余的不是。

 #include <stdio.h>
#include <math.h>
#define SIZE 5
int
main(void)
{
    FILE *in = fopen("pfile1", "r");
    int x[SIZE], y[SIZE], i;
    double h, polygon;
    printf("x Values   y Values\n");
    if (NULL != in){
        for (i=0;i<SIZE;++i)
            fscanf(in, "%d %d", &x[i], &y[i]);
        for (i=0;i<SIZE;++i)
            printf("%4d %10d\n", x[i], y[i]);
        fscanf(in, "%lf", &h);
        printf("height = %lf\n", h);
    }
    fclose(in);
    polygon = poly_area(x, y, h);
    printf("The area of the polygon is: %.4lf meters squared", polygon);
    return(0);
}
void
tri_side(int xArr[SIZE] , int yArr[SIZE], double *n)
{
    double line[7];
    line[0] = sqrt(pow((xArr[1] - xArr[0]),2) + pow((yArr[1] - yArr[0]),2));
    line[1] = sqrt(pow((xArr[2] - xArr[1]),2) + pow((yArr[2] - yArr[1]),2));
    line[2] = sqrt(pow((xArr[3] - xArr[2]),2) + pow((yArr[3] - yArr[2]),2));
    line[3] = sqrt(pow((xArr[4] - xArr[3]),2) + pow((yArr[4] - yArr[3]),2));
    line[4] = sqrt(pow((xArr[4] - xArr[0]),2) + pow((yArr[4] - yArr[0]),2));
    line[5] = sqrt(pow((xArr[4] - xArr[1]),2) + pow((yArr[4] - yArr[1]),2));
    line[6] = sqrt(pow((xArr[4] - xArr[2]),2) + pow((yArr[4] - yArr[2]),2));
    *n = line[0];
}
void
tri_area(int xArr[SIZE], int yArr[SIZE], double *n)
{
    double triArea1, triArea2, triArea3, s, a, b, c;
    double *p, sides, triArea[3];
    tri_side(xArr, yArr, &sides);
    p = &sides;
    a = *p;
    b = *(p+1);
    c = *(p+5);
    s = (a + b + c)/2;
    triArea1 = sqrt(s*(s-a)*(s-b)*(s-c));
    triArea[0] = triArea1;
    a = *(p+1);
    b = *(p+5);
    c = *(p+6);
    s = (a + b + c)/2;
    triArea2 = sqrt(s*(s-a)*(s-b)*(s-c));
    triArea[1] = triArea2;
    a = *(p+2);
    b = *(p+3);
    c = *(p+6);
    s = (a + b + c)/2;
    triArea3 = sqrt(s*(s-a)*(s-b)*(s-c));
    triArea[2] = triArea3;
    *n = triArea[0];
}
double
pent_area(int xArr[SIZE], int yArr[SIZE])
{
    int i;
    double pentArea, area1, area2, area3, areas, *p;
    tri_area(xArr, yArr, &areas);
    p= &areas;
    pentArea = *(p) + *(p+1) + *(p+2);
    printf("Pentagon Area :%lf\n", pentArea);
    return(pentArea);
}
int
poly_area(int xArr[SIZE], int yArr[SIZE], double h)
{
    double pentArea, polyArea;
    pentArea = pent_area(xArr, yArr);
    polyArea = h * pentArea;
    return(polyArea);
}

2 个答案:

答案 0 :(得分:0)

你不能传回指向堆栈分配内存的指针,即

double *p, sides, triArea[3];

然后尝试按照以下方式将其传递回来

*n = triArea[0];

这意味着在此代码中

double pentArea, area1, area2, area3, areas, *p;
tri_area(xArr, yArr, &areas);
p= &areas;
pentArea = *(p) + *(p+1) + *(p+2);

p + 1无效。当一个函数调用返回它用于创建局部变量的堆栈上的内存不再在范围内,即你不能使用它们。如果你想使用函数中的某些东西,你需要将它传入并使用它,或者在函数的堆上分配它并传回去。

另外。将指针传递给数组时,可以按如下方式声明它们

pent_area(int xArr[], int yArr[])

我这个案子改变了这个

tri_area(int xArr[SIZE], int yArr[SIZE], double *n)

到这个

tri_area(int xArr[], int yArr[], double triAreas[])

答案 1 :(得分:0)

谢谢大家,我明白了。

    #include <stdio.h>
    #include <math.h>
    #define SIZE 5
    int
    main(void)
    {
        FILE *in = fopen("pfile1", "r");
        int x[SIZE], y[SIZE], i;
        double h, polygon;
        printf("x Values   y Values\n");
        if (NULL != in){
            for (i=0;i<SIZE;++i)
                fscanf(in, "%d %d", &x[i], &y[i]);
            for (i=0;i<SIZE;++i)
                printf("%4d %10d\n", x[i], y[i]);
            fscanf(in, "%lf", &h);
            printf("height = %lf\n", h);
        }
        fclose(in);
        polygon = poly_area(x, y, h);
        printf("The area of the polygon is: %.4lf meters squared", polygon);
        return(0);
    }
    void
    tri_side(int xArr[] , int yArr[], double line[7])
    {
        line[0] = sqrt(pow((xArr[1] - xArr[0]),2) + pow((yArr[1] - yArr[0]),2));
        line[1] = sqrt(pow((xArr[2] - xArr[1]),2) + pow((yArr[2] - yArr[1]),2));
        line[2] = sqrt(pow((xArr[3] - xArr[2]),2) + pow((yArr[3] - yArr[2]),2));
        line[3] = sqrt(pow((xArr[4] - xArr[3]),2) + pow((yArr[4] - yArr[3]),2));
        line[4] = sqrt(pow((xArr[4] - xArr[0]),2) + pow((yArr[4] - yArr[0]),2));
        line[5] = sqrt(pow((xArr[4] - xArr[1]),2) + pow((yArr[4] - yArr[1]),2));
        line[6] = sqrt(pow((xArr[4] - xArr[2]),2) + pow((yArr[4] - yArr[2]),2));
    }
    void
    tri_area(int xArr[], int yArr[], double triArea[3])
    {
        double s, a, b, c, triSides[7];
        tri_side(xArr, yArr, triSides);
        a = triSides[0];
        b = triSides[1];
        c = triSides[5];
        s = (a + b + c)/2;
        triArea[0]= sqrt(s*(s-a)*(s-b)*(s-c));
        a = triSides[1];
        b = triSides[5];
        c = triSides[6];
        s = (a + b + c)/2;
        triArea[1] = sqrt(s*(s-a)*(s-b)*(s-c)); 
        a = triSides[2];
        b = triSides[3];
        c = triSides[6];
        s = (a + b + c)/2;
        triArea[2] = sqrt(s*(s-a)*(s-b)*(s-c));
    }
    double
    pent_area(int xArr[], int yArr[])
    {
        int i;
        double pentArea, areas[3];
        tri_area(xArr, yArr, areas);
        pentArea = areas[0]+areas[1]+areas[2];
        printf("Pentagon Area :%lf\n", pentArea);
        return(pentArea);
    }
    int
    poly_area(int xArr[], int yArr[], double h)
    {
        double pentArea, polyArea;
        pentArea = pent_area(xArr, yArr);
        polyArea = h * pentArea;
        return(polyArea);
    }