Thread: How do you copy one array in C to another?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    Not actually quite true: it decays to a pointer that is not an lvalue. In this case a distinction without a difference (but it allows arguments to be passed without losing const, so that's important).
    Yes, that is true.

    Quote Originally Posted by transgalactic2
    so its a const pointer
    it cannot be changed in value.
    However, this is still not true. It is an array, not a pointer of any sort. Granted, the difference only shows up in the use of sizeof, but there is a semantic difference nonetheless.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it's an array, and not a pointer, therefore you cannot assign an address to it.
    If you pass an array to a function, a pointer to its first element is passed instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    wow... I started an arguement lol, I have looked-up memcpy(), but only found syntax and not examples of how it should be used. However it is late now and I shall post back in the morning. And yes by the way 'same-age' is an array.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cus View Post
    wow... I started an arguement lol, I have looked-up memcpy(), but only found syntax and not examples of how it should be used.
    I am worried that you believe there is a difference.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by tabstop View Post
    I am worried that you believe there is a difference.
    a difference to what exactly?

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Very well, I shall make an example for you.

    You've looked up memcpy, so you know that it will copy n bytes from the source pointed to by the second argument to the destination pointed to from the first argument. Simply calculate your bytes.

    Code:
    #include <string.h>
    int main()
    {
        int age[4] = {59, 89, 23, 20};
        int sameAge[4];
    
        memcpy(sameAge, age, sizeof age[0] * 4);
    
        return 0;
    }
    Like that. But a loop would work just as well. memcpy is essentially that in a function.

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cus View Post
    a difference to what exactly?
    A difference between the syntax and how it should be used.

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    thank you very much for your useful post, the information that memcpy(), is essentially a functions explains fully. And also answers the question to my previous post to 'tabstop'

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, in this case you could just write:
    Code:
    memcpy(sameAge, age, sizeof sameAge);
    but the multiplication would be more like what you would do if this were in another function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    thanks all

    thanks for the help, i have done it this way:

    Code:
    #include <string.h>
    #include <stdio.h>
    int main()
    {
        int age[4] = {169, 8, 17, 20};
        int sameAge[4];
        int i;
    
    memcpy(sameAge, age, sizeof age);
    for( i=0; i<4; i++)
    {	
    printf("%d\n", sameAge[i]);
    }
        return 0;
    }
    Last edited by cus; 01-03-2009 at 08:01 AM.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Consequently, it can also be written as:
    Code:
    memcpy( sameAge, age, sizeof(age) );
    Which is my preferred method. It also saves you headaches since sizeof something does not work for everything, but sizeof(something) always works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    yes thanks, i think i just read that on wikipedia, due to the type name always need to be enclosed in parenthesis i.e.

    printf("%u,%u", (unsigned) sizeof c, (unsigned) sizeof(int) );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a copy of an array by copying its address..
    By transgalactic2 in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 11:07 AM
  2. Copy constructor for Queue ADT (as an Array)
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2007, 11:05 PM
  3. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  4. copy contents of array to single value?
    By mapunk in forum C Programming
    Replies: 3
    Last Post: 12-02-2005, 09:28 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM