Thread: copying array?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    1

    copying array?

    I have a problem I want to copy an array to another one .. have any one any idea about this ... ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Sounds like a school project.

    Why do you ask? What seems to be the problem you are having that you believe you need to do this? Can you post some code?
    Last edited by Syndacate; 07-30-2011 at 04:11 AM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    You can copy the contents of one array into another using memcpy or memmove. (Use memmove if the boundaries of both arrays overlap).

    man page: memcpy

    An example:

    Code:
    #include <string.h>
    
    int main(void)
    {
    	int src[] = { 20, 30, 40, 50, 60, 70, 80, 90 };
    	int dest[sizeof src / sizeof src[0]];
    
    	memcpy(dest, src, sizeof dest);
    	return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MrSandwich View Post
    You can copy the contents of one array into another using memcpy or memmove.
    Please review this board's homework policy ...
    --> Click the red text --> Announcements - General Programming Boards

    We do not as a rule simply give people answers. When it is a school assignment the general requirement is that they should start the programming on their own and come here only if they are stuck (real stuck, not lazy stuck). You do nobody any good service by simply handing out answers; the handout neither teaches them to think about the process nor enjoins them to learn more about programming.

    Old saying: "Give a man a fish and you've fed him for one day. Teach a man how to catch fish and you've fed his village for a lifetime."

    Be a teacher, not a cheater!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying row array help!!
    By problem in forum C Programming
    Replies: 1
    Last Post: 04-22-2011, 05:51 PM
  2. Copying an array into another
    By Alienchicken in forum C Programming
    Replies: 4
    Last Post: 04-16-2011, 02:41 PM
  3. Copying an array into another
    By Alienchicken in forum C Programming
    Replies: 0
    Last Post: 04-13-2011, 09:55 PM
  4. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  5. array copying
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 01:43 AM