gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:08:13 24 4
gpt4 key购买 nike

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

我需要计算高度,即像素高度/像素宽度,但我无法正确获取指针的语法。我想在第三个(高度)函数中调用前两个函数的指针。

#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;
}

有人能看出哪里出了问题吗?

最佳答案

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

#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

关于c - 如何在C中的第三个函数中使用来自2个不同函数的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com