need help assigning one structure to another using pointers
Hi,
i have two pointer to structures named *currMB and *PrevMB and i want to assign PrevMB into currMB.
will the C++ code below work.
struct Macroblock *currMB;
struct Macroblock *PrevMB;
*currMB= & PrevMB;
and if i want to average these two structures and put the output in currMB.
then can i write
currMB = (currMB+PrevMB)/2;
Re: need help assigning one structure to another using pointers
No, it will fail (crash)
You must instantiate Macroblock objects, not just the pointers that point to none/nowhere.
Re: need help assigning one structure to another using pointers
Quote:
Originally Posted by
naeem561
Hi,
i have two pointer to structures named *currMB and *PrevMB and i want to assign PrevMB into currMB.
will the C++ code below work.
struct Macroblock *currMB;
struct Macroblock *PrevMB;
*currMB= & PrevMB;
and if i want to average these two structures and put the output in currMB.
then can i write
currMB = (currMB+PrevMB)/2;
How do you average a struct? Conceptually that doesn't make much sense.
Re: need help assigning one structure to another using pointers
As victor said, they are only pointers currently pointing to nothing.
Code:
struct Macroblock *currMB = new Macroblock;
struct Macroblock *PrevMB = new Macroblock;
Now they point to a Macroblock (don't forget to delete them as soon as you are done with them).
Code:
currMB = (currMB+PrevMB)/2;
To do this you need to implement your own = + and / operator. Assuming that your struct contains some numbers you can average.
Re: need help assigning one structure to another using pointers
Well.. the program will not compile itself!
You cannot add two pointers.