473,842 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assigning values to an array after declaring it

Very basic question I'm afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

myarray[0]=8;
myarray[1]=3;
myarray[2]=4;
myarray[3]=0;
myarray[4]=0;
myarray[5]=1;
myarray[6]=8;
myarray[7]=8;
etc...

I can't assign values immediately on declaration because I want myarray
to be initialised differently according to various cases only determined
at runtime.

Thanks for any help. best regards -Eric

Nov 14 '05
14 74923
Peter Ammon <pe*********@ro cketmail.com> wrote:
Eric Bantock wrote:
Very basic question I'm afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

[snip] Initializers can be determined at runtime. Why can't you initialize
where you define it? You can use an inner scope. void foo(void) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
{
float arr[] = {a, b, c};
/* do something with arr */
}
}


Thanks, but in my case it's much simpler to declare the array in question
as a global, so I don't think I can do something like this--my a, b and c
will only be determined much later, as a result of user input etc.
Nov 14 '05 #11
In <ll************ *******@twister 01.bloor.is.net .cable.rogers.c om> "nobody" <no****@nowhere .non> writes:
"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:2u******* *************** **********@4ax. com...
On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
<eb@example.com > wrote:
>Very basic question I'm afraid. Once an array has been declared, is there
>a less tedious way of assigning values to its members

(than assigning values to members)

No.

Well, you could memcpy data into it, if you could be sure of byte order

and
alignment constraints.

But that wouldn't be *assigning* (as in "assigning values to its members"),
would it?


Why not? Isn't copying the values of the members of one array into the
corresponding members of another arrays the closest you can get in
the way of "array assignment", an operation not directly supported
by the language?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
In <ca**********@u s23.unix.fas.ha rvard.edu> Eric Bantock <eb@example.com > writes:
is what I'll do. Actually, I have always wondered (but never been curious
enough to actually find out) exactly *why* C won't let you assign values
"all at once" to an array except at declaration. Can anyone explain?


After declaration, memcpy(array1, array2, sizeof array1) does exactly
that. It may not look as nicely as array1 = array2, but it has the same
semantics as this hypothetical usage of the assignment operator.

If you want to know why array1 = array2 doesn't work, it is because
in this context (as in most other contexts) array names are converted to
pointers to their first elements. Furthermore, these pointers are not
lvalues, so they cannot be assigned values (it wouldn't make any sense
to try to change the address of the first element of an array, anyway).

It is because of this rule that arrays are not considered first class
citizens in C. You cannot pass them to functions and you cannot
return them from functions. Instead, you pass the address of the first
array element to the function and you can return such an address from the
function, which, most of the time, is exactly what you want.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #13
On Wed, 16 Jun 2004 17:01:07 -0700, Peter Ammon
<pe*********@ro cketmail.com> wrote:
Eric Bantock wrote:
Very basic question I'm afraid. Once an array has been declared, is there
a less tedious way of assigning values to its members than the following:

myarray[0]=8;
myarray[1]=3;
etc...
Initializers can be determined at runtime. Why can't you initialize

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^where you define it? You can use an inner scope.

void foo(void) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
{
float arr[] = {a, b, c};
/* do something with arr */
}
}


This is C99 only, correct?
--
aib

ISP e-mail accounts are good for receiving spam.
Nov 14 '05 #14
On Tue, 22 Jun 2004 03:51:44 GMT, Orhan Kavrakoglu
<ga*******@ttne t.net.tr> wrote:
On Wed, 16 Jun 2004 17:01:07 -0700, Peter Ammon
<pe*********@ro cketmail.com> wrote:

Initializers can be determined at runtime. Why can't you initialize

^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^
where you define it? You can use an inner scope. {
float arr[] = {a, b, c};
/* do something with arr */
}
}


This is C99 only, correct?


For an aggregate (list) initializer, yes; in C89 all aggregate
initializer values must be constant=compil e-time expressions
regardless of the (scope and) duration of the variable. Although of
course runtime could still be allowed for auto as an extension.

A single-expression initializer, including for a struct or union, of
an auto variable, can be runtime since C89 at least.
- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #15

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

Similar topics

3
8284
by: Ben | last post by:
Hi all, This may sound easy but I'm having trouble assigning values to array element. My problem is as follows: m = for o in m: # Here first o is 'Peter'.. I want to do something like this: Peter = 10
2
3393
by: Zitan Broth | last post by:
Greetings All, Running pg 7.3.4 and was reading: http://archives.postgresql.org/pgsql-interfaces/2003-09/msg00018.php . Basically want to assign values to an array and then a 2d array. However I can't get this to run in properly I get a syntax error (at or near ", output_txt_arr TEXT, output_str text ); CREATE OR REPLACE FUNCTION F_TEST(TEXT) RETURNS NUMERIC AS '
2
3277
by: ryoung | last post by:
I receive the following error when I attempt to assign values to a web service array. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 553: AppDTO.zip = sZip Line 554: AppDTO.dob = sDOB '** line 555 is the highlighted row with the problem.
5
1663
by: Manuel Graune | last post by:
Hello, while trying to learn how to program using objects in python (up to now simple scripts were sufficient for my needs) I stumbled over the a problem while assigning values to an object. The following piece of code shows what I intend to do: <---snip--->
17
18777
by: Cliff | last post by:
Hi, I'm in the process of porting some code from a 3rd party and have hit a problem with the following: typedef struct { unsigned char Red; unsigned char Green; unsigned char Blue; }TBoxColour;
1
4799
by: Nightfarer | last post by:
Hello. I have a big trouble using System.Array class (it's the first time I use it) for a software I'm developing. I have a form with a textbox(numbers) and one button (done). Once the number is added to textbox clicking the done button saves the number in a variable (Numbers). The next form has a textbox(letter) and two buttons (add new, done). Values inserted in textbox are the values to be added to Array clicking add new button (the...
2
4053
by: Ray D. | last post by:
I want to set matrix A with the values below, but it produced a syntax error when I try to compile the commented code. I was told to use a for loop to do this (as shown below), but that really isn't possible considering I need these specific values. Is there a way to do this in one statement instead of multiple statements of A = ..., where I manually specify the position? Why can you initialize like this with a static array, but can't...
6
2899
by: sumuka | last post by:
Hello, I'm doing a project in java and im not able to assign the values which are got from for loop to the 2-dimensional array. Can anyone tell me how to assign the values and print them? the code is: gr.getGridArray(); for (int s=0;s<=NoOfElevations;s++) for (int t=0;t<=NoOfElevations;t++)
9
2159
by: J. Peng | last post by:
I just thought python's way of assigning value to a variable is really different to other language like C,perl. :) Below two ways (python and perl) are called "pass by reference", but they get different results. Yes I'm reading 'Core python programming', I know what happened, but just a little confused about it. $ cat t1.py def test(x):
0
9870
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
9715
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
10940
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...
0
10610
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10308
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...
0
9451
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7854
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
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3141
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.