Copying pointers in structures instead of the structure data?

This is a discussion on Copying pointers in structures instead of the structure data? within the C++ Programming forums, part of the General Programming Boards category; How is this done? I don't quite understand....

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Copying pointers in structures instead of the structure data?

    How is this done? I don't quite understand.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That would depend on what you are actually trying to achieve.

    But here's an example:
    Code:
    struct widget
    {
       ... some data - fairly large. 
    };
    
    struct gadget
    {
       struct widget *widgetPtr;
    ...
    };
    
    
    widget a;
    widget b;
    
    gadget g;
    
    ...
    
    somefunc(int flag)
    {
        if (flag)
            g.widgetPtr = &a;
        else
            g.widgetPtr = &b;
    ...
    }
    The advantage over storing a copy of the struct inside gadget is that we can set it to a or b using a simple pointer move. Copying the whole struct itself would involve a lot more data to be copied (if the struct is large).

    I used global variables above to make the code small - it is not a recommended method, but it makes the example short.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    I have these structs:

    Code:
    struct Listnode
    {	
    	char lastname[30];
    	char firstname[30];
    	int age;
    };
    
    struct Listtype
    {	
    	Listnode* person[30];
    	int size;
    };
    and this function:

    Code:
    void doInsert (Listtype &List)
    {
    	char first[30]; //entry firstname
    	char last[30]; //entry lastname
    	int years; //entry age
    	int number; //slot number to place record at
    	int i;
    
    	system("cls");
    	cout<<"Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE"<<endl;
    	cout<<"\n";
    	cin>>first>>last>>years;
    	cout<<"Insert this record at what position: ";
    	cin>>number;
    
    
    	for (i=List.size+1; i>number; i--) //moving all records down 1 position to make room
    	{
    		List.person[i]=List.person[List.size];
    	}
    	List.person[number]->firstname=first;
    	List.person[number]->lastname=last;
    	List.person[number]->age=years;
    	List.size=i;
    }
    Its a function to insert records into the struct... And if you insert somewhere where there already is a record I just want the function to make sure that you move them so that nothing is erased... But I get an error that the "left operand must be an L-value" at these 2 lines:

    List.person[number]->firstname=first;
    List.person[number]->lastname=last;
    I'm a little confused as to whether what I'm attempting here is a copy of the structure data or copying the pointer. And why I'm getting that specific error.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What you're trying to do is assign to an array. Perhaps you mean to strcpy instead.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    What you're trying to do is assign to an array. Perhaps you mean to strcpy instead.
    Code:
    	{
    		List.person[i]=List.person[i-1];
    	}
    Here, am I copying data or copying a pointer?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Sparrowhawk View Post
    Code:
    	{
    		List.person[i]=List.person[i-1];
    	}
    Here, am I copying data or copying a pointer?
    person is an array of pointers, so you are copying pointers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    person is an array of pointers, so you are copying pointers.
    Got it. thanks

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    right now I have:

    Code:
    void doInsert (Listtype &List)
    {
    	char first[30]; //entry firstname
    	char last[30]; //entry lastname
    	int years; //entry age
    	int number; //slot number to place record at
    	int i;
    
    	system("cls");
    	cout<<"Enter a record in this format: LASTNAME <SPACE> FIRSTNAME <SPACE> AGE"<<endl;
    	cout<<"\n";
    	cin>>first>>last>>years;
    	cout<<"Insert this record at what position: ";
    	cin>>number;
    
    
    	for (i=List.size; i>number-1; --i) //moving all records down 1 position to make room
    	{
    		List.person[i]=List.person[i-1];
    	}
    	strcpy(List.person[number-1]->firstname, first); //placing record in number slot
    	strcpy(List.person[number-1]->lastname, last);
    	List.person[number-1]->age=years;
    	List.size++;
    }
    for some reason this is causing an output where the new record to be inserted appears at List.person[number] and at List.person[number+1]

    I don't know why either... I tried outputting the loop verbosely in a separate program which outputs:

    4 copies to 5
    3 copies to 4
    2 copies to 3

    and thats the end of the loop. Which is correct if number = 3. In the actual program the record is copied twice though at number 3 and at number 4 for some reason.

    [edit] - I figured it out I think, but I don't know why it happens or how I can "fix it"... The problem arises I'm thinking because when I assign a value at List.person[number-1]... that pointer points to 2 positions in the list after the function is done... I'm not sure why though..., shouldn't there just be garbage there if I moved everything else?

    [edit2] - Got it... The problem was what I thought it was... I just made a new Listnode object there so it wasn't being pointed too again.
    Last edited by Sparrowhawk; 02-23-2009 at 06:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Data Structures Debugging
    By 0rion in forum C Programming
    Replies: 15
    Last Post: 08-28-2004, 11:36 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM
  5. Help require for some data structure topics
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-15-2002, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21