gcc c: how do I impliment and reference a 2d array of structs
I am to implement an array that holds a color image. he image has 640x480 pixels. So, I need a 2-d array. Each pixel has 3 colors, red green blue. The values for each color is an integer between 0 and 255. So, I need an array of structs to hold the data.
I am having trouble with the struct:
typedef struct tnode
{
int red;
int green;
int blue;
} myimage(640,480);
//I have to use the typedef - my interpretation of the above is that is creates a 2 dimensional array of the structure which has integer elements of red, green, and blue//
//Further in the program inside a loop I have://
myimage[b][a].red=rand()%256;
//The compile says that there is a syntax error before the [. b and a are counters in for/next loops and I have stdlib.h included so I can use the rand() function//
Is my typedef correct? Does this create a 2 dimensional array called myimage with red, green, and blue elements?
I am reasonably certin my myimage[b][a].red=.. is correct. Is the compiler saying there's a syntax error because myimage is not properly defined as a 2 dimensional array stuct?
Please help. Thanks.
....
I'm still struggling to get the correct syntax for the structure. In another section of this program, I am trying to write an element of the struct to a file.
This is the code I have.
/*Write the Array to file*/
for (a=0;479;i++)
{
for (b=0;639;i++)
{
fprintf(fileptr,myimage[b][a].red);
fprintf(fileptr,int myimage[b][a].green);
fprintf(fileptr,int myimage[b][a].blue); */
}
fprintf(fileptr,"n");
/*inserts CR after end of every row*/
}
fclose(fileptr);
}
--- The compiler says that there is a syntax error before int or before myimage (If I leave out the INT). There's something wrong with the way I am addressing the data to be read out. What is the correct syntax to write out each element?
Thanks. (In case this all gets crammed together, attached is my question and my entire program so far)
By OTA: Poramate Tarasak, PhD (IP)
OTA Rating: 4.9/5
Your Price: $2.19 (original value ~$15.96)
What's included:
Page generated in 0.0107 seconds