如何在C中的第三个函数中使用来自2个不同函数的指针

时间:2016-05-29 05:00:58

标签: c arrays function pointers multidimensional-array

我正在尝试编写一个计算“图像”属性的程序。 “图像”是一个0和1的2D数组,0是白色空间,1是黑色像素。

我需要计算高度,这只是pixelHeight / pixelWidth,但我无法得到指针的语法正确。我想在我的第三个(高度)函数中调用前两个函数的指针。

#include <stdio.h>
#include <math.h>

#define MAX_X       20
#define MAX_Y       20

int countBlackPixels(int image[MAX_X][MAX_Y]);
int findYPixel(int image[MAX_X][MAX_Y]);
int findXPixel(int image[MAX_X][MAX_Y]);
double findTallness(int image[MAX_X][MAX_Y]);

//function prototypes here

void processImage(int image[MAX_X][MAX_Y]) {

    int nBlackPixels;
    nBlackPixels = countBlackPixels(image);
    printf("Pixel-count: %d\n", nBlackPixels);

    int *pixelHeight;
    *pixelHeight = findYPixel(image);
    printf("Height: %d\n", *pixelHeight);

    int *pixelWidth;
    *pixelWidth = findXPixel(image);
    printf("Width: %d\n", *pixelWidth);

    double tallness;
    tallness = findTallness(tallness);
    printf("Tallness: %.6lf\n", tallness);
}

//Count Black Pixels
int countBlackPixels(int image[MAX_X][MAX_Y]) {
    int x, y, blackPixelCount;
    blackPixelCount = 0;
    x = 0;
    while (x < MAX_X) {
        y = 0;
        while (y < MAX_X) {
            if (image[x][y] == 1) {
                blackPixelCount = blackPixelCount + 1;
                //printf("black pixel at x=%d y=%d\n", x, y);  //  example debug printf
            }
            y = y + 1;
        }
        x = x + 1;
    }
    return blackPixelCount;
}

//Height
//Loops through from bottom-left to the right, and then up
//through the Y axis, find the last occurance of Y, 
//adds one to overcome array's zero count, and
//returns this value as the pixel height

    int * findYPixel(int image[MAX_X][MAX_Y]) {
        int x, y, *pixelHeight;
        y = 0;
        *pixelHeight = -1;
        while (y < MAX_X) {
            x = 0;
            while (x < MAX_X) {
                if (image[x][y] == 1) {
                    *pixelHeight = y+1;
                    }
                x = x + 1;
            }
            y = y + 1;
        }
        return pixelHeight;
    }

//Width
//Loops through from bottom-left to the top, and then across
//through the X axis, find the last occurance of X, adds 1
//to deal with array 0 count, and
//returns this value as the pixel width

    int * findXPixel(int image[MAX_X][MAX_Y]) {
        int x, y, *pixelWidth;
        x = 0;
        while (x < MAX_X) {
            y = 0;
            while (y < MAX_Y) {
                if (image[x][y] == 1) {
                    *pixelWidth = x+1;
                    }
                y = y + 1;
            }
            x = x + 1;
        }
        return pixelWidth;
    }


//Tallness function
    double findTallness(int *pixelWidth, int *pixelHeight) {
        double tallness;
        tallness = *pixelWidth / *pixelHeight;
        printf("tall=%.6lf\n", tallness);

        return tallness;
    }

任何人都可以看到它出错的地方吗?

1 个答案:

答案 0 :(得分:1)

请检查您的功能声明。它们与函数定义不兼容。我修改了代码,使其编译如下

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_X       20
#define MAX_Y       20

int countBlackPixels(int image[MAX_X][MAX_Y]);
int* findYPixel(int image[MAX_X][MAX_Y]);
int* findXPixel(int image[MAX_X][MAX_Y]);
double findTallness(int *,int *);

//function prototypes here

void processImage(int image[MAX_X][MAX_Y]) {

    int nBlackPixels;
    nBlackPixels = countBlackPixels(image);
    printf("Pixel-count: %d\n", nBlackPixels);

    int *pixelHeight;
    pixelHeight = findYPixel(image);
    printf("Height: %d\n", *pixelHeight);

    int *pixelWidth;
    pixelWidth = findXPixel(image);
    printf("Width: %d\n", *pixelWidth);

    double tallness;
    tallness = findTallness(pixelWidth, pixelHeight);
    printf("Tallness: %.6lf\n", tallness);
}

//Count Black Pixels
int countBlackPixels(int image[MAX_X][MAX_Y]) {
    int x, y, blackPixelCount;
    blackPixelCount = 0;
    x = 0;
    while (x < MAX_X) {
        y = 0;
        while (y < MAX_X) {
            if (image[x][y] == 1) {
                blackPixelCount = blackPixelCount + 1;
                //printf("black pixel at x=%d y=%d\n", x, y);  //  example debug printf
            }
            y = y + 1;
        }
        x = x + 1;
    }
    return blackPixelCount;
}

//Height
//Loops through from bottom-left to the right, and then up
//through the Y axis, find the last occurance of Y, 
//adds one to overcome array's zero count, and
//returns this value as the pixel height

    int * findYPixel(int image[MAX_X][MAX_Y]) {
        int x, y, *pixelHeight;
        pixelHeight = malloc(sizeof(int));
        y = 0;
        *pixelHeight = -1;
        while (y < MAX_X) {
            x = 0;
            while (x < MAX_X) {
                if (image[x][y] == 1) {
                    *pixelHeight = y+1;
                    }
                x = x + 1;
            }
            y = y + 1;
        }
        return pixelHeight;
    }

//Width
//Loops through from bottom-left to the top, and then across
//through the X axis, find the last occurance of X, adds 1
//to deal with array 0 count, and
//returns this value as the pixel width

    int * findXPixel(int image[MAX_X][MAX_Y]) {
        int x, y, *pixelWidth;
        pixelWidth = malloc(sizeof(int));
        x = 0;
        while (x < MAX_X) {
            y = 0;
            while (y < MAX_Y) {
                if (image[x][y] == 1) {
                    *pixelWidth = x+1;
                    }
                y = y + 1;
            }
            x = x + 1;
        }
        return pixelWidth;
    }


//Tallness function
    double findTallness(int *pixelWidth, int *pixelHeight) {
        double tallness;
        tallness = (1.0 * *pixelWidth) / *pixelHeight;
        printf("tall=%.6lf\n", tallness);

        return tallness;
    }

您没有分配内存来存储findXPixelfindYPixel中的整数。要分配内存,请按如下方式初始化变量:

pixelHeight = malloc(sizeof(int));

类似地, pixelWidth = malloc(sizeof(int));

此外,在processImage中, 您无需取消引用指针*pixelHeight*pixelWidth。 将它们用作

pixelHeight = findYPixel(image);
pixelWidth = findXPixel(image);

另外,将tallness = *pixelWidth / *pixelHeight;替换为

tallness = (*pixelWidth * 1.0)/ *pixelHeight;

执行此类计算时,您需要将int投射到double

Demo