473,396 Members | 1,891 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,396 software developers and data experts.

Can C++ allow multiple array assignment?

For example,
int arr[5];

arr = {1,2,3,4,5};

May 2 '06 #1
11 14851
Mr. Ken wrote:
For example,
int arr[5];

arr = {1,2,3,4,5};


Next time, put your question in the message instead of in the subject.

No, this is not possible, except for initialization:

int arr[5] = {1,2,3,4,5}; // ok

You may consider using std::vector instead of arrays, they are more
versatile and safe, though they do not support this kind of assignment
either.
Jonathan

May 2 '06 #2
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Mr. Ken wrote:
For example,
int arr[5];

arr = {1,2,3,4,5};


Next time, put your question in the message instead of in the subject.

No, this is not possible, except for initialization:

int arr[5] = {1,2,3,4,5}; // ok

You may consider using std::vector instead of arrays, they are more
versatile and safe, though they do not support this kind of assignment
either.
Jonathan


Thank you Jonathan.
What I need is a one-liner to initialize arr into some values. will the
following code work?
int arr[5];
int dummy[5] = {1,1,1,1,1};

for (i = 0; i < 5; i = i + 1) {
*arr = dummy;
arr[i] = others;
// other codes involving array arr;
}

May 2 '06 #3
Mr. Ken wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
Mr. Ken wrote:
For example,
int arr[5];

arr = {1,2,3,4,5};
Next time, put your question in the message instead of in the subject.

No, this is not possible, except for initialization:

What I need is a one-liner to initialize arr into some values. will the
following code work? int arr[5];
int dummy[5] = {1,1,1,1,1};

for (i = 0; i < 5; i = i + 1) {
Prefer:

for (int i=0; i<5; ++i)
*arr = dummy;
This is illegal, you are assigning an int* to an int.
arr[i] = others;
Well, depending on what 'others' is, it may or not be legal.
// other codes involving array arr;
}


I don't understand exactly what you are looking for. Please provide a
full example of what you are trying to accomplish.

Perhaps this?

# include <algorithm>

void f(int dummy[5])
{
int arr[5];
std::copy(dummy, dummy+5, arr);
}

Jonathan

May 2 '06 #4
Mr. Ken wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Mr. Ken wrote:
For example,
int arr[5];

arr = {1,2,3,4,5};

Next time, put your question in the message instead of in the subject.

No, this is not possible, except for initialization:

int arr[5] = {1,2,3,4,5}; // ok

You may consider using std::vector instead of arrays, they are more
versatile and safe, though they do not support this kind of assignment
either.
Jonathan


Thank you Jonathan.
What I need is a one-liner to initialize arr into some values. will the
following code work?
int arr[5];
int dummy[5] = {1,1,1,1,1};

for (i = 0; i < 5; i = i + 1) {
*arr = dummy;
arr[i] = others;
// other codes involving array arr;
}


What is "others"? If you're trying to copy dummy to arr you can do the
following in one line:

std::copy(dummy,dummy+sizeof(dummy)/sizeof(*dummy),arr)

You'll also need to #include <algorithm>

cf. http://www.sgi.com/tech/stl/copy.html
May 2 '06 #5
int arr[5];
int dummy[5] = {1,1,1,1,1};

for (i = 0; i < 5; i = i + 1) {
Prefer:

for (int i=0; i<5; ++i)
*arr = dummy;


This is illegal, you are assigning an int* to an int.


Is this correct syntax?
arr = dummy;
// This assigns the base address of dummy to arr.
// Will "arr[i] = 2; " alter the content of dummy[i]?
arr[i] = others;
Well, depending on what 'others' is, it may or not be legal.


others = 200*i/123;
// other codes involving array arr;
}
I don't understand exactly what you are looking for. Please provide a
full example of what you are trying to accomplish.


What I need is, in each loop, the array is initialized to {1,1,1,1,1}, then
a few elements of the
array is altered. Subsequent codes will depend on the content of the array
to work properly.


Perhaps this?

# include <algorithm>

void f(int dummy[5])
{
int arr[5];
std::copy(dummy, dummy+5, arr);
}

Jonathan


Does std::copy work with old versions of gcc-2.95?

Thank you Jonathan.

May 3 '06 #6
>
What is "others"? If you're trying to copy dummy to arr you can do the
following in one line:

std::copy(dummy,dummy+sizeof(dummy)/sizeof(*dummy),arr)

You'll also need to #include <algorithm>

cf. http://www.sgi.com/tech/stl/copy.html


others are some integer values, thus "arr[i] = others;" alters the content
of a
an element.

The company's software are very outdated, does std::copy work with gcc-2.95
in Unix shell?
May 3 '06 #7
Mr. Ken wrote:

What I need is, in each loop, the array is initialized to {1,1,1,1,1}, then
a few elements of the
array is altered. Subsequent codes will depend on the content of the array
to work properly.

If you really want all elements initialized to the same value then
std:vector has a constructor which does this.
Does std::copy work with old versions of gcc-2.95?


This I don't know. It would be easy to write a little program to test
it though.
May 3 '06 #8
Mr. Ken wrote:
int arr[5];
int dummy[5] = {1,1,1,1,1};

for (i = 0; i < 5; i = i + 1) {
Prefer:

for (int i=0; i<5; ++i)
*arr = dummy;


This is illegal, you are assigning an int* to an int.


Is this correct syntax?
arr = dummy;


No. You cannot assign arrays.
// This assigns the base address of dummy to arr.
// Will "arr[i] = 2; " alter the content of dummy[i]?
No, this makes no sense. Arrays are not pointers, pointers are not
arrays.
arr[i] = others;


Well, depending on what 'others' is, it may or not be legal.


others = 200*i/123;


Ok, I suppose others is [convertible to] an int.
// other codes involving array arr;
}


I don't understand exactly what you are looking for. Please provide a
full example of what you are trying to accomplish.


What I need is, in each loop, the array is initialized to {1,1,1,1,1}, then
a few elements of the
array is altered. Subsequent codes will depend on the content of the array
to work properly.


Just use a loop, it's simple and it works.
Does std::copy work with old versions of gcc-2.95?


Dunno, you should ask them. It probably does, although 2.95 is what, 10
years old?
Jonathan

May 3 '06 #9

"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

Just use a loop, it's simple and it works.
Does std::copy work with old versions of gcc-2.95?


Dunno, you should ask them. It probably does, although 2.95 is what, 10
years old?
Jonathan


Yeah, I am using a loop anyhow. In the manual, it says latest updated on
1993.

May 3 '06 #10
Mr. Ken wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Just use a loop, it's simple and it works.
Does std::copy work with old versions of gcc-2.95?

Dunno, you should ask them. It probably does, although 2.95 is what, 10
years old?
Jonathan


Yeah, I am using a loop anyhow. In the manual, it says latest updated on
1993.


Unfortunately 2.95 (2.96, actually) is the version that VxWorks uses.

May 3 '06 #11
Mr. Ken wrote:
"Jonathan Mcdougall" <jo***************@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...

Just use a loop, it's simple and it works.
Does std::copy work with old versions of gcc-2.95?


Dunno, you should ask them. It probably does, although 2.95 is what, 10
years old?
Jonathan


Yeah, I am using a loop anyhow. In the manual, it says latest updated on
1993.


Nice, were there keyboards at that time? :)
Jonathan

May 3 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LS | last post by:
1. When I select an assignment, the class roster disappears. I don't want it to show up until I select an assignment. 2. I want to be able to enter grades and submit all with only one submit...
20
by: Petter Reinholdtsen | last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The problematic part is '("a")'. I am sure 'char a = "a";' is valid ANSI C, but I am more unsure if it is allowed to place () around the...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
10
by: Patrick F | last post by:
Hi, How come that a programming language not yet 10years old dosen't support Multiple inherritances?, I got 2 classes, both with alot of fields and methods that i need from my third class, so i...
20
by: quantumred | last post by:
I found the following code floating around somewhere and I'd like to get some comments. unsigned char a1= { 5,10,15,20}; unsigned char a2= { 25,30,35,40}; *(unsigned int *)a1=*(unsigned int...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
14
by: =?Utf-8?B?RWR3YXJk?= | last post by:
Hi everybody, To me the following code shouldn't work but it does ! Imports system.String Dim x As String="This,is,a,test" Dim y(1) As String y=x.Split(",") TextBox1.text=y(3) why "Y" which is...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.