Results 1 to 4 of 4

Thread: Assigning an array to an array

  1. #1
    Join Date
    Nov 2009
    Posts
    36

    Assigning an array to an array

    Could someone explain to me why we can not assign an array of type char to an array of char?

    An example to be clear:

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
        char word[25];
        char copyword[25];
        cin >> word;
        word = copyword;
        cout << copyword;
    }
    the above code gives me this error:

    In function `int main()':|error: ISO C++ forbids assignment of arrays|
    ||=== Build finished: 1 errors, 0 warnings ===|
    Another example:
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
        char matrix [20][20];
        char word[25];
        cin >> word;
        for (int i = 0; word[i] != '\0'; i++)
        {
            matrix [i] = word;
        }
    }
    I do not understand why we can not do that. Can someone explain it to me?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Assigning an array to an array

    For compatibility with C++ for which that can be compatible with earlier language that had a model of different picture.

    That's why things like boost::array is not bad because it gives a true semantics of first order in C++ array. It is safer to use boost::array then a plain array, because it is not a pointer in disguise. It is much faster than normal array.

  3. #3
    Join Date
    Nov 2009
    Posts
    36

    Re: Assigning an array to an array

    Can you to explain me what is "boost:: array"? How is it used? What can do what? An example would be more than enough to understand the concept of boost:: array.

    Header <boost/array.hpp> consists of the below code:

    Code:
    namespace boost {
      template<typename T, std::size_t N> class array;
      template<typename T, std::size_t N> void swap(array<T, N>&, array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator==(const array<T, N>&, const array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator!=(const array<T, N>&, const array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator<(const array<T, N>&, const array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator>(const array<T, N>&, const array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator<=(const array<T, N>&, const array<T, N>&);
      template<typename T, std::size_t N> 
        bool operator>=(const array<T, N>&, const array<T, N>&);
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Assigning an array to an array

    boost:: array template is a class that behaves exactly like an array in terms of behavior and performance but also gives him a real semantic value (copy, placement, etc.).

    Usage example:

    Code:
    #include <boost/array.hpp>
    
    int main()
    {
      const int size = 5;
      boost::array<double,size> arr;
      for (int p=0; p!=size+1; ++p) arr.at(p)=p; //Checks range
      for (int i=0; i!=size+1; ++i) arr[i]=i; //Does not check range
      return 0;
    }

Similar Threads

  1. Array of array in PHP
    By cyber-noob in forum Software Development
    Replies: 4
    Last Post: 26-02-2010, 05:13 PM
  2. C# array help
    By Daren in forum Software Development
    Replies: 5
    Last Post: 03-01-2010, 07:12 AM
  3. Problems with a 2D array
    By Preetish in forum Software Development
    Replies: 3
    Last Post: 15-10-2009, 11:58 PM
  4. XML to ARRAY
    By Vandam in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 06:14 PM
  5. JAVA array
    By Daren in forum Software Development
    Replies: 2
    Last Post: 06-03-2009, 06:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,435,140.57071 seconds with 16 queries