Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-11-2009
Member
 
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:

Quote:
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?
Reply With Quote
  #2  
Old 18-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 18-11-2009
Member
 
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>&);
}
Reply With Quote
  #4  
Old 18-11-2009
Member
 
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Assigning an array to an array"
Thread Thread Starter Forum Replies Last Post
Array of array in PHP cyber-noob Software Development 4 26-02-2010 05:13 PM
C# array help Daren Software Development 5 03-01-2010 07:12 AM
Problems with a 2D array Preetish Software Development 3 15-10-2009 11:58 PM
XML to ARRAY Vandam Software Development 3 29-06-2009 06:14 PM
JAVA array Daren Software Development 2 06-03-2009 06:13 PM


All times are GMT +5.5. The time now is 08:20 PM.