In pset4, resizing an image, I decided to try out pointers and memory allocation to learn more about it, but I've run into an issue with my approach...
I've created a pointer and allocated memory for it. Then I have created another pointer of the same type and assigned it the address of the first.
RGBTRIPLE *img = malloc(bi.biSizeImage * sizeof(RGBTRIPLE));
RGBTRIPLE *ptr;
ptr = img;
I then read the original image and repeat each pixel n times:
RGBTRIPLE triple;
fread(&triple, sizeRGB, 1, inptr);
for (int w = 0; w < n; w++, ptr++)
{
*ptr = triple;
}
So far, so good...
When it then comes to the padding, I run into the issue of trying to save (or even skip) 0 to 3 BYTE's and not RGBTRIPLE's.
How can I assign a value of type BYTE
at the address of ptr
?
I've tried casting like so, but it yields an error
((BYTE *)ptr)++
// assignment to cast is illegal, lvalue casts are not supported
EDIT
Perhaps my question is not clear. If I allocate some amount of memory of a certain type, like RGBTRIPLE
, will the memory blocks then be treated differently, than if I allocated memory using BYTE
type?
Would these two, memory wise, result in the same?
RGBTRIPLE *a = malloc(10 * sizeof(RGBTRIPLE));
BYTE *b = malloc(30 * sizeof(BYTE));
And if so, can I then increment *a
by one BYTE
and not one RGBTRIPLE
a += 1; // not what I want...
a += b; // possible?