0

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?
0

Not completely getting what you want, why don't you access the individual bytes using ptr->rgbtBlue and so on?

Also, you could create another pointer

BYTE *ptrByte = (BYTE *)ptr;

and use and increment that one, if you want.

2
  • I ended up using a pointer of type BYTE and almost doing as you suggest by writing each rgbt value into memory byte by byte. – Morten Dec 7 '16 at 18:23
  • I edited my question, is it more clear now? – Morten Dec 7 '16 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .