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

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    How do you copy one array in C to another?

    Hi all, this problem doesn't involve strings.

    I have written the following:

    Code:
    #include <stdio.h>
    int main()
    { 
    int age[4];
    int same_age[4];
    age[0]=23;
    age[1]=34;
    age[2]=65;
    age[3]=74;
    
    same_age=age;
    
    printf("%d\n", same_age[0]);
    printf("%d\n", same_age[1]);
    printf("%d\n", same_age[2]);
    printf("%d\n", same_age[3]);
    return 0;
    }
    However this returns an error stating: 'incompatible types in assignment'?
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You cannot write to an array, only to an individual cell in an array. So you must copy into same_age[0], into same_age[1], ..., ..., ....

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    thanks for reply, but how, i don't want to do it individually.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use memcpy() or a loop.
    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

  5. #5

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i thought memcpy() was a function for strings?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cus
    i thought memcpy() was a function for strings?
    An array of ints can be viewed as an array of bytes when you are not interested in the individual ints at that point.
    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

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i have no idea how to use memcpy()

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cus View Post
    i have no idea how to use memcpy()
    So look it up.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you can use "reference"
    when you write
    Code:
    same_age=age;
    or
    Code:
    same_age=&age[0];
    that way same age will point to the same starting place of address as "age"
    and same_age will be the same as age.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by transgalactic2 View Post
    you can use "reference"
    when you write
    Code:
    same_age=age;
    or
    Code:
    same_age=&age[0];
    that way same age will point to the same starting place of address as "age"
    and same_age will be the same as age.
    I feel obligated to point out that you cannot, in fact, do this. It is quite simply not possible to assign anything at all to same_age. Not a value, not a "reference" (whatever that is supposed to mean).

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    same_age is a pointer (points to the first cell of the array)
    i put in it the address of the first cell of age (&age[0])

    Code:
    same_age=&age[0];
    so theoretically i cant see why it cant happen?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    same_age, by itself, does indeed decay to a const pointer to the first cell of the array. And I admit that I (as well as others, alas) often elide the word "const" in the description. But nevertheless, it is a const pointer -- which means its value cannot be changed.

    Edit: 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).
    Last edited by tabstop; 01-02-2009 at 01:13 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    same_age is a pointer (points to the first cell of the array)
    No, same_age is an array, not a pointer. It is true that an array is converted to a pointer to its first element when passed as an argument, but in the scope of the main function same_age is an array, and one cannot assign to an array.

    EDIT:
    Quote Originally Posted by tabstop
    And I admit that I (as well as others, alas) often elide the word "const" in the description. But nevertheless, it is a const pointer -- which means its value cannot be changed.
    The 1999 edition of the C standard omits the word "const", hence there is no elision in the first place.
    Last edited by laserlight; 01-02-2009 at 12:02 PM.
    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

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so its a const pointer
    it cannot be changed in value.

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