473,899 Members | 2,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can C++ allow multiple array assignment?

For example,
int arr[5];

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

May 2 '06 #1
11 14975
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.goo glegroups.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.goo glegroups.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(d ummy)/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(d ummy)/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******** *************@u 72g2000cwu.goog legroups.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

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

Similar topics

1
1395
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 button. This is my code. File is loaded at http://lynnesmith.net/teacher/entergrades.php +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ <HTML> <HEAD> <TITLE>Gradebook</TITLE> <LINK REL="STYLESHEET" TYPE="text/css"...
20
7106
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 string literal.
5
2243
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 C++Builder, where I have the option to 'Run' it), I can't run multiple files, since the Run option is grayed out if one cpp file is already running. I have another option of running the multiple files: from the command prompt (i.e. opening a...
18
2276
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
10
3215
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 tried: class myClass3 : myClass1, myClass2 { and got an error says that i cant. How can C# be a compete with C++ if you cant even make a simple thing like that?
20
3546
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 *)a2; // now a1=a2, a1=a2, etc.
2
2521
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 the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
10
5473
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 this simple case.) /---- int array = { 1, 2, 3, 4 } \-----
14
1007
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 an array of lenght 2 accepts index 3 which is larger than its lenght? Any thoughts?
0
9997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.