473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I pass an array to a constructor?

I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage.

Here's a simplified version of what I'm working on:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. class board
  4. {
  5.     public:
  6.         int state[64];
  7.         board(int arr[])
  8.         {
  9.             *state = *arr;
  10.         }
  11.         void print();
  12. };
  13.  
  14. void board::print()
  15. {
  16.     for (int y=0; y<8; y++)
  17.     {
  18.         for (int x=0; x<8; x++)
  19.             std::cout << state[x + y*8] << " ";
  20.         std::cout << "\n";
  21.     }
  22. }
  23.  
  24. int main()
  25. {
  26.     int test[64] = {
  27.         0, 1, 2, 3, 4, 5, 6, 7,
  28.         1, 2, 3, 4, 5, 6, 7, 8,
  29.         2, 3, 4, 5, 6, 7, 8, 9,
  30.         3, 4, 5, 6, 7, 8, 9,10,
  31.         4, 5, 6, 7, 8, 9,10,11,
  32.         5, 6, 7, 8, 9,10,11,12,
  33.         6, 7, 8, 9,10,11,12,13,
  34.         7, 8, 9,10,11,12,13,14 };
  35.  
  36.     board b(test);
  37.     b.print();
  38.  
  39.     std::cin.get();
  40.     return 0;
  41. }
  42.  
Can someone explain why this doesn't work and how to properly pass an array? Notes: I don't want to copy the array. Also, I'm using MSVC++ 2010 Express.
Feb 24 '12 #1
1 7737
I found the solution after posting to stackoverflow:

"The name of an array is the address of the first element in it.

Hence the line *state = *arr will set state[0] to arr[0].

Since right now you have defined state as int state[64];, state is const pointer of type int whose address cannot be changed.

You can change it to int *state; and then state = arr will work."
Feb 24 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: John | last post by:
I would like to pass array variables between URLs but I don't know how. I know its possible with sessions, but sessions can become useless if cookies are disabled. I have unsuccessfully tried...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
0
by: sebascomeau | last post by:
Hi everyone, Hello my name is Sebastien and I need some help. He have one week past and I search to do one thing but I can't. If someone help me, I'll be so happy :). My problem is I want to...
3
by: Boni | last post by:
Dear all, I have written a C++ com object: STDMETHOD(myfunc)(INT* array) Now I need to pass an array from c# to com. I generated a COM interop and it the signature of the function is ...
6
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
0
by: Jagdish | last post by:
Hello, Every body I have recently joined this group and I want to know how to pass Array of string to a Com object function.. Actually I want to pass String array from VB6 to Com Interface...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.