Thread: need help assigning one structure to another using pointers

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    Post 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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by naeem561 View Post
    *currMB= & PrevMB;
    This will probably be a compiler error, but if not you would be assigning the address of PrevMB to the content of currMB, which is not what you want.

    Code:
    currMB = PrevMB;
    Both pointers will point to the same struct. If you want to copy the contents:
    Code:
    memcpy(currMB,PrevMB,sizeof(struct Macroblock));
    then can i write

    currMB = (currMB+PrevMB)/2;
    For sure not.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    *currMB is a struct, while &PrevMB is a pointer to a pointer to a struct, so they're not comparable at all.

    (currMB+PrevMB)/2 is the average of the addresses stored in those variables (since that's what currMB and PrevMB are storing, addresses).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. silence warning when assigning pointers
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 11:18 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. How To Detect Pointers In Structure
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 07-31-2003, 04:12 PM