gpt4 book ai didi

c - C代码的指针

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:02 24 4
gpt4 key购买 nike

//
// main.c
// cbp
//
// Created by Zhan on 16/4/13.
// Copyright © 2016年 Zhan. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include<math.h>

#pragma pack(2)

typedef unsigned short int WORD;
typedef unsigned int DWORD;

typedef struct BMP_FILE_HEADER
{
WORD bType;
DWORD bSize;
WORD bReserved1;
WORD bReserved2;
DWORD bOffset;
} BMPFILEHEADER;

typedef struct BMP_INFO
{
DWORD bInfoSize;
DWORD bWidth;
DWORD bHeight;
WORD bPlanes;
WORD bBitCount;
DWORD bCompression;
DWORD bmpImageSize;
DWORD bXPelsPerMeter;
DWORD bYPelsPerMeter;
DWORD bClrUsed;
DWORD bClrImportant;
} BMPINF;

/*彩色表*/
typedef struct RGB_QUAD
{
WORD rgbBlue;
WORD rgbGreen;
WORD rgbRed;
WORD rgbReversed;
} RGBQUAD;

int main()
{
FILE *fp,*fpout;
BMPFILEHEADER fileHeader;
BMPINF infoHeader;
int width, height;
//WORD c;
int s;
float s1,s2;
unsigned char **image;

if((fp = fopen("XG.bmp", "rb")) == NULL)
{
printf("Cann't open the file!\n");
}


fseek(fp, 0, 0);
fread(&fileHeader, sizeof(fileHeader), 1, fp);
fread(&infoHeader, sizeof(infoHeader), 1, fp);

printf("size fileHeader == %ld\n",sizeof(fileHeader));
printf("size infoHader == %ld\n",sizeof(infoHeader));

width = infoHeader.bWidth;
height = abs(infoHeader.bHeight);

printf("infoHeader.bWidth == %d\n",width);
printf("infoHeader.bheight == %d\n",height);

image = (unsigned char **)malloc(sizeof(unsigned char *) * width);
image[0] = (unsigned char *)malloc(sizeof(unsigned char ) * width * height);

// unsigned char **image1 =(unsigned char**) malloc (width*sizeof(int*));
// for(int i=0;i<width;i++)
// *image1++=(unsigned char*) malloc (height*sizeof(int));

fseek(fp,(sizeof(fileHeader)+sizeof(infoHeader)),SEEK_SET);
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++){
fread(&image[i][j],1,1,fp); }} //Memory leak ?
for(int i=1;i<width;i++){
for(int j=1;j<height;j++)
{
s1=image[i-1][j+1]+2*image[i][j+1]+image[i+1][j+1];
s1=s1-image[i-1][j-1]-2*image[i][j-1]-image[i+1][j-1];
s2=image[i-1][j-1]+2*image[i-1][j]+image[i-1][j+1];
s2=s2-image[i+1][j-1]-2*image[i+1][j]-image[i+1][j+1];
s=fabs(s1)+fabs(s2);
if(s>80)
image[i][j]=255;
else
image[i][j]=0;
}
}
if((fpout=fopen("za.bmp","wb"))==NULL){
printf("cannot open outfile!\n");
free(image[0]);
free(image);
return 0;
}
printf("intoWrite\n");
fwrite(&fileHeader,sizeof(fileHeader),1,fpout);
fwrite(&infoHeader,sizeof(infoHeader),1,fpout);
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++){
printf("forWrite\n");
fwrite(&image[i][j],1,1,fpout);}}
// c = fgetc(fp);
// int mi=0,mj=0;
// while (!feof(fp))
// {
// fwrite(&c,1,1,fpout);
// mi++;mj++;
// c = fgetc(fp);
// }
free(image[0]);
free(image);
printf("OK!\n");
fclose(fp);
fclose(fpout);
return 0;
}

这是我的bmp边缘检测代码。我用Xcode运行这段代码,它总是提示“Thread 1:EXC_BAD_ACCESS(code=1,address = 0x0)”或“Function call argument is an uninitialized value”at“fread(&image[i][j],1,1,fp)"。为什么我用静态二维数组没有错误,但是动态二维数组有错误?

最佳答案

这将为每个 width 分配 height,您现在应该能够引用 image[i][j]

int each = 0;
image = malloc(sizeof(unsigned char *) * width);
for ( each = 0; each < width; each++) {
image[each] = malloc(sizeof(unsigned char ) * height);
}

释放将是相似的。

for ( each = 0; each < width; each++) {
free ( image[each]);
}
free ( image);

关于c - C代码的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36593838/

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