Thread: how can i copy one struct into another?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    how can i copy one struct into another?

    I have two structs

    Code:
    struct boardstate{
    	int board[9][9];
    	int wpawns, wknights, wbishops, wrooks, wqueens, wkings;
    	int bpawns, bknights, bbishops, brooks, bqueens, bkings;
    	int wkcastle, wqcastle, bkcastle, bqcastle; 
    	int drawcounter;
    	int legalmoves;  
    	int wenpassant[2], benpassant[2]; //[0]=i; [1]=j
    	int whitekcastle, whiteqcastle, blackkcastle, blackqcastle;
    	int wkingloc[2], bkingloc[2];
    	char absoluteboard[9][9]; //used to record pieces which cannot move due to absolute pins
    	int dangerboard[9][9]; //used to record squares which the king cannot move to
    	int reversedangerboard[9][9]; //used to record squares which the non-moving king cannot move to
    	int allpossiblemoves[65][65]; //lists all possible move targets [y] for piece at square [x], equal to value of piece at [x]
    	int allpossiblepositions[65][65]; //lists all possible move targets [y] for piece at square [x], equal to value of position at p[z]
    	int wincheck; int bincheck;
    	enum whoseturn color;
    	int turn;
    	double evaluation;
    	double bestmoveeval;
    	int loci, locj, targeti, targetj; //records to-from squares for piece which just moved
    }; 
    
    
    extern struct boardstate p[100];
    extern struct boardstate X;
    I want to make a copy of p[0], which is why I created X. Is there a way to copy everything from p[0] into X, or do I have to transfer each variable independently? Somewhere I read that memcopy might somehow do this, but I haven't seen anything which shows the necessary syntax.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    memcpy will do the trick if you don't want to use the assignment operator. Note that it won't actually copy the contents of things pointed at, rather it would duplicate the contents of the pointer (make them point to the same space).
    Quote Originally Posted by Adam Rinkleff View Post
    I read that memcopy might somehow do this, but I haven't seen anything which shows the necessary syntax.
    You mean that you were too lazy to look up the memcpy function to see how to use it?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    But how do I use memcpy? I can see on C++ Reference that it is of the format:

    Code:
    void * memcpy ( void * destination, const void * source, size_t num );
    But what do I actually type? For example... size_t num? I don't know what that is. So how do I actually use it to copy from p[0] to X?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by quzah View Post
    You mean that you were too lazy to look up the memcpy function to see how to use it?


    Quzah.
    No...I mean I don't understand how to use memcpy, because I've never used it before.. Of course I've looked it up, obviously I have, which is why I've mentioned it. Clearly I put some effort into this before coming here and asking about it. But no, I don't know how to use it. That's why I asked.

    I really don't see why you have to be so rude. I'm trying to learn something, which means I don't already know the answers. If you want to help, thanks, great. If not, please refrain from being rude. I'm obviously asking how to use memcopy to copy [0] into X; either you know or don't. I'm asking for help, but I don't need to be harassed about being too "lazy" to figure it out. If I was lazy, I wouldn't be trying to figure it out.
    Last edited by Adam Rinkleff; 07-05-2011 at 03:51 PM.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You may want to take a look at the sizeof() function for that.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adam Rinkleff View Post
    No...I mean I don't understand it. Yes, I've looked it up, obviously I have, which is why I've mentioned it. No, I don't know how to use it. That's why I asked. I really don't see why you have to be so rude. I'm trying to learn something, which means I don't already know the answers. If you want to help, thanks, great. If not, please refrain from being rude. I'm obviously asking how to use memcopy to copy [0] into X; either you know or don't. I'm asking for help, but I don't need to be harassed about being too "lazy" to figure it out. If I was lazy, I wouldn't be trying to figure it out.

    Welll ... you could type something like .... memcopy(target, source, sizeof(target));

    target being th name of the struct you are copying to and source being the one you are copying from ... and yes, they do have to be the same size.

    You might also consider giving your variables more descriptive names than p and X...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    sizeof(p[0]) is 35024, is that the size of the structure regardless of the values within it, or do those change?


    Should I just do:
    Code:
    memcpy ( X, p[0], sizeof(p[0]));
    ?

    Yah I guess that should work, sizeof(X) is also 35024. Thanks.
    Last edited by Adam Rinkleff; 07-05-2011 at 03:57 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adam Rinkleff View Post
    sizeof(p[0]) is 35024, is that the size of the structure regardless of the values within it, or do those change?


    Should I just do:
    Code:
    memcpy ( X, p[0], sizeof(p[0]));
    ?

    Well, gosh... why don't you give that a try and let us know if you run into trouble...
    (IOW... a little confidence is often a very good thing.)

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    I did try it:


    incompatible type for argument 1 of `memcpy'
    incompatible type for argument 2 of `memcpy'

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Or you could just do:
    Code:
    # include <stdio.h>
    # include <string.h>
    
    int main(void){
    
    	struct myStruct{
    		int a;
    		int b;
    	};
    	myStruct myStructA;
    	myStruct myStructB;
    
    	myStructA.a = 5;
    	myStructA.b = 6;
    
    	myStructB = myStructA;
    
    	printf("%d %d", myStructB.a, myStructB.b);
    
    	getchar();
    	return(0);
    }
    Look, everyone here means well. Sometimes people just come across short and to the point. Sarcasm and jokes are hard to implement through messages. My advice for all future endeavors is not to take anything personally and just look for the advice given. Trust me, there is plenty to learn from the people here, even if it comes across somewhat curt.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by AndrewHunter View Post

    X=p[0];
    That seems a lot easier, why then would I ever want to use memcpy instead of that?

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adam Rinkleff View Post
    I did try it:


    incompatible type for argument 1 of `memcpy'
    incompatible type for argument 2 of `memcpy'
    Did you look at the instructions for memcpy() again?

    You will note that it needs pointers...

    memcpy(&X, &p[0], sizeof(p[0]));
    Last edited by CommonTater; 07-05-2011 at 04:06 PM.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adam Rinkleff View Post
    That seems a lot easier, why then would I ever want to use memcpy instead of that?
    Once again... go ahead and give it a try...

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by CommonTater View Post

    memcpy(&X, &p[0],sizeof(p[0]);
    Oh, I was trying *X, *p[0], but going back to post #10 -- why use memcpy at all if I can just use what AndrewHunter suggested?

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    That would be because memcpy's arguments expect pointers and not objects. So you would implement it:
    Code:
    memcpy(&x, p, sizeof(p[0]));
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mapped section, struct copy
    By punkywow in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2009, 09:35 AM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  3. copy struct contents
    By bandal27 in forum C Programming
    Replies: 9
    Last Post: 01-09-2009, 03:01 PM
  4. How to copy one struct in C to another?
    By cus in forum C Programming
    Replies: 20
    Last Post: 01-03-2009, 01:22 PM
  5. copy struct to class
    By guda in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2003, 09:27 PM