[英]Dynamically allocate 2-dimensional structure array in C
我一直在嘗試根據 CS50 問題集 4 創建一個邊緣過濾器。我已經看到了幾種解決方案,但是我想知道我的方法是否可行。 我正在嘗試通過一個像素寬度的黑色邊框來擴展輸入圖像。 為此,我想將我的二維 RGBTRIPLE 結構在任一側擴展一個像素。 我在第一行將 RGB 的所有值設置為 0(又名黑色),然后將原始圖像復制到臨時結構中,用各自的顏色替換除邊框值之外的所有值。
我正在定義一個可變長度的二維結構 RGBTRIPLE,其中包含數據類型 BYTE 的三個值:
RGBTRIPLE temp[height+2][width+2] = {};
我收到錯誤消息,由於長度可變,它可能尚未初始化,我理解。 我已經看到了幾種使用指針和malloc的解決方案,我希望在第一行中正確實現了這些解決方案。 我一直在嘗試按照以下兩行將 RGBTRIPLE 連接到指針:
RGBTRIPLE *ptr = (RGBTRIPLE *)malloc((height+2)*(width+2)*sizeof(RGBTRIPLE));
RGBTRIPLE temp[height+2][width+2] = &ptr;
temp[height+2][width+2] = {0};
在這里將所有值設置為零也不起作用,但這是另一個問題。
我想在for 循環中使用原始的 RGBTRIPLE,但我無法讓它工作。 我看到的所有示例都在之后使用指針來添加任何信息。 有什么方法可以使用 malloc 定義 RGBTRIPLE,以便之后我可以在代碼中將其用作結構的“正常”元素,如temp[][]所示:
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
temp[i+1][j+1] = image[i][j];
}
}
for(int i = 1; i <= height; i++)
{
for(int j = 1; j <= width; j++)
{
int counter = 0;
float gxr, gxb, gxg, gyr, gyb, gyg = 0;
//right pixel
gxb += (2*temp[i][j+1].rgbtBlue);
gxg += (2*temp[i][j+1].rgbtGreen);
gxr += (2*temp[i][j+1].rgbtRed);
等所有周圍的像素。
任何幫助表示贊賞。
您可以按照以下示例代碼進行初始化。
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char BYTE;
typedef struct tagRGBTRIPLE
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} RGBTRIPLE;
int main()
{
int height = 400;
int width = 600;
RGBTRIPLE img[height][width];
RGBTRIPLE temp[height+2][width+2];
for (int i = 0; i < height; i++) /* Build a sample image file */
{
for (int j = 0; j < width; j++)
{
img[i][j].rgbtRed = 68;
img[i][j].rgbtGreen = 188;
img[i][j].rgbtBlue = 32;
}
}
for (int i = 0; i < (height + 2); i++) /* Initialize the temporary RGBTRIPLE structure*/
for (int j = 0; j < (width + 2); j++)
{
temp[i][j].rgbtRed = 0;
temp[i][j].rgbtGreen = 0;
temp[i][j].rgbtBlue = 0;
}
for(int i = 0; i < height; i++) /* Imported code from the issue */
{
for(int j = 0; j < width; j++)
{
temp[i+1][j+1] = img[i][j];
}
}
for(int i = 0; i <= (height + 2); i++) /* Right and left edges*/
{
float gxr = 0, gxb = 0, gxg = 0;
temp[i][0].rgbtRed = gxr;
temp[i][0].rgbtGreen = gxg;
temp[i][0].rgbtBlue = gxb;
temp[i][width + 1].rgbtRed = gxr;
temp[i][width + 1].rgbtGreen = gxg;
temp[i][width + 1].rgbtBlue = gxb;
}
for(int i = 0; i <= (width + 2); i++) /* Top and bottom edges */
{
float gyr = 0, gyb = 0, gyg = 0;
temp[0][i].rgbtRed = gyr;
temp[0][i].rgbtGreen = gyg;
temp[0][i].rgbtBlue = gyb;
temp[height + 1][i].rgbtRed = gyr;
temp[height + 1][i].rgbtGreen = gyg;
temp[height + 1][i].rgbtBlue = gyb;
}
/* See what we have at a pixel point */
printf("Top edge RGBTRIPLE %d, %d, %d \n", temp[0][144].rgbtRed, temp[0][144].rgbtGreen, temp[0][144].rgbtBlue);
printf("Left edge RGBTRIPLE %d, %d, %d \n", temp[144][0].rgbtRed, temp[144][0].rgbtGreen, temp[144][0].rgbtBlue);
printf("RGBTRIPLE within image %d, %d, %d \n", temp[144][144].rgbtRed, temp[144][144].rgbtGreen, temp[144][144].rgbtBlue);
return 0;
}
C 並沒有真正提供初始化元組的簡單方法,因此您可能需要“for”循環來執行此操作。 試驗一下這個場景,看看它是否適用於你的項目精神。
一些問題:
盡可能使用mem...()
。
零字節填充整個可變長度數組:
// RGBTRIPLE temp[height+2][width+2] = {};
RGBTRIPLE temp[height+2][width+2];
memset(temp, 0, sizeof temp);
我在第一行將 RGB 的所有值設置為 0(又名黑色),然后將原始圖像復制到臨時結構中,用各自的顏色替換除邊框值之外的所有值。
選擇:
// Given image[][] is a 2D array
for(int i = 0; i < height; i++) {
memcpy(temp[i+1], image[i], sizeof image[i]);
}
正確初始化
float gxr, gxb, gxg, gyr, gyb, gyg = 0;
只初始化gyg
。
float gxr = 0;
float gxb = 0;
...
float gyg = 0;
高級: int
數學與size_t
數學
int*int*size_t
可能會溢出int*int
而size_t*int*int
不會溢出。
C 中不需要鑄造。
大小為引用的 object,而不是類型。
// RGBTRIPLE *ptr = (RGBTRIPLE *)malloc((height+2)*(width+2)*sizeof(RGBTRIPLE));
RGBTRIPLE *ptr = malloc(sizeof ptr[0] * (height+2) * (width+2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.