How do you copy one array in C to another?

This is a discussion on How do you copy one array in C to another? within the C Programming forums, part of the General Programming Boards category; Hi all, this problem doesn't involve strings. I have written the following: Code: #include <stdio.h> int main() { int age[4]; ...

  1. #1
    cus
    cus is offline
    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,185
    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
    cus
    cus is offline
    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
    21,589
    Use memcpy() or a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595

  6. #6
    cus
    cus is offline
    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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    cus
    cus is offline
    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,185
    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,185
    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,185
    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 12:13 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    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 11:02 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

Page 1 of 2 12 LastLast
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, 10: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, 08:28 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

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