CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    1

    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;

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    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.
    Victor Nijegorodov

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: need help assigning one structure to another using pointers

    Quote Originally Posted by naeem561 View Post
    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.

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: need help assigning one structure to another using pointers

    Well.. the program will not compile itself!
    You cannot add two pointers.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured