Thread: why can't a static array copy values from a dynamic array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    why can't a static array copy values from a dynamic array

    but it can the other way around
    Code:
    static_Array= dynamic_Array;
    
    dynamic_Array = static_Array;
    


    the second statement works and i'm able to print out both arrays with equal values

    but with the first

    [cod
    e]
    static_Array = dynamic_Array;I get
    incompatible types in assignment of 'int*' to 'int [7]' is the error I get
    [/code]





  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by c++noob145 View Post
    but it can the other way around
    Code:
    static_Array= dynamic_Array;
    
    dynamic_Array = static_Array;
    


    the second statement works and i'm able to print out both arrays with equal values

    but with the first

    [cod
    e]
    static_Array = dynamic_Array;I get
    incompatible types in assignment of 'int*' to 'int [7]' is the error I get
    [/code]




    In C, you can NOT copy arrays; I think this also applies to C++.

    You are assigning a pointer to point to the other array based on your error message.
    You need to figure out the correct way to copy an array in C++.
    In C, it is strcpy or memcpy.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by stahta01 View Post
    In C, you can NOT copy arrays; I think this also applies to C++.
    Correct for C arrays (there are C++ arrays, too, namely std::array [C+11 addition]).

    So first, the language forbids that you copy a C array into another one. That's a relic from C. You can copy a C++ array into another:

    Code:
    std::array<int, 10> a1, a2;
    a1 = a2;
    a2 = a1;
    Secondly, you name "dynamic array."
    But the thing is, you are just using dynamic memory and assigning the address at the start of the array to a pointer. How is the compiler supposed to know what the pointer is actually pointing to? It doesn't. That is why you cannot assign a pointer to an array, regardless of what that pointer is pointing to. Another relic from C.
    You cannot assign a fixed array to a dynamic array, either. You think you can, but you can't. The reality is that the address of the first element in the array is copied into the pointer. So the array the pointer is pointing is lost to the void and you get a memory leak. Another relic from C.

    You can copy a static array into a dynamic array and vice versa in C++. It is a little tricky, however, but here is how to do it:

    Code:
    std::vector<int> v; // Dynamic vector
    // Fill v with some stuff
    std::array<int, 10> arr;
    std::copy(v.begin(), v.begin() + arr.size(), arr.begin()); // Important note: std::copy is stupid and will do a buffer overrun if you don't specify the bounds properly, so don't use v.end().
    The reverse is also possible in different ways. The safest way is:

    Code:
    #include <iterator>
    #include <vector>
    #include <array>
    #include <algorithm>
    
    int main()
    {
    	std::array<int, 10> arr;
    	std::vector<int> v;
    	std::copy(arr.begin(), arr.end(), std::back_inserter(v));
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 02:29 PM
  2. Copy array into a Dynamic array
    By pantherman34 in forum C Programming
    Replies: 15
    Last Post: 05-01-2010, 10:58 PM
  3. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  4. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  5. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM