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 | | | | re: assigning values to an array after declaring it
Eric Bantock wrote:[color=blue]
> 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
>[/color]
Here are some ideas:
1. Many constant arrays:
Declare constant arrays for each case in the program.
Copy the data from the constant array to the variable
depending on the circumstances. If the data won't change,
then use a pointer instead of an array.
2. Load data from a stream (file).
Prefer a text file to a binary file. Text files can be
easily created and updated using an editor. Binary files
are more difficult to create and maintain.
3. Load data from another source.
Some platforms have places that programs can store
their data, such as a registry or ROM. Accessing these
are off-topic for this newsgroup and not portable.
--
Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book | | | | re: assigning values to an array after declaring it
On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
<eb@example.com> wrote:
[color=blue]
>Very basic question I'm afraid. Once an array has been declared, is there
>a less tedious way of assigning values to its members[/color]
(than assigning values to members)
No.
Well, you could memcpy data into it, if you could be sure of byte order and
alignment constraints.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =--- | | | | re: assigning values to an array after declaring it
Eric Bantock wrote:[color=blue]
> 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.[/color]
You can use a loop:
for (i = 0; i < N; ++i)
myarray[i] = some_expression_probably_involving_i;
Of course, this approach requires that there is some method
behind the apparent madness of 8,3,4,...
If the array contents are not systematic or readily computed
but have been laboriously typed in from historical records of
molybdenum production in nineteenth-century Bolivia *but* there
are only a few such sets of data, you could carry a few pre-
initialized arrays around and then copy the chosen one into
myarray[] at run-time:
const SomeType data_sets[][N] = {
{ 8,3,4, ... },
{ 6,6,8, ... },
{ 4,9,2, ... },
...
};
SomeType myarray[N];
...
int k = index_of_chosen_data_set;
memcpy(myarray, data_sets[k], sizeof myarray);
It's hard to recommend one approach over the other (or
over alternative possibilities) without some notion of what
you're really trying to accomplish.
-- Eric.Sosman@sun.com | | | | re: assigning values to an array after declaring it
In <capqhe$knc$1@us23.unix.fas.harvard.edu> Eric Bantock <eb@example.com> writes:
[color=blue]
>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.[/color]
If there are only a few possibilities, create an array for each one:
static const int ini1[] = { ... };
static const int ini2[] = { ... };
static const int ini3[] = { ... };
Then, if you have decided that ini2 is the right initialiser, you can do:
memcpy(myarray, ini2, sizeof myarray);
The other solution is a compound literal, but since this works in C99 only
I'm not advising it.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: assigning values to an array after declaring it
Eric Bantock wrote:
[color=blue]
> 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
>[/color]
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 */
}
} | | | | re: assigning values to an array after declaring it
"Mark McIntyre" <markmcintyre@spamcop.net> wrote in message
news:2us0d0p3ulkrhofbj1tejl5vqajgl3fp74@4ax.com...[color=blue]
> On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
> <eb@example.com> wrote:
>[color=green]
> >Very basic question I'm afraid. Once an array has been declared, is there
> >a less tedious way of assigning values to its members[/color]
> (than assigning values to members)
>
> No.
>
> Well, you could memcpy data into it, if you could be sure of byte order[/color]
and[color=blue]
> alignment constraints.
>[/color]
But that wouldn't be *assigning* (as in "assigning values to its members"),
would it? IMHO, as usual, problem lies in
the problem definition, which seems not to define the problem, but to imply
the solution., and question turns out to be about the implementation of
particular
"suggested" solution. I hate it when my bosses are doing this to me. So the
1st
answer is perfectly correct (to my limited knowledge). Second one will
hopefully
clarify OP's question to himself. Though it may be considered irritating to
some participants here, I go by the old Chinese proverb: "Give a man a fish
and
you feed him for a day. Teach a man to fish, and you feed him for a
lifetime..",
even if someone only asks for one (topical) fish, (non-topical) reply in
some
circumstances can be more beneficial, in the long run. | | | | re: assigning values to an array after declaring it
Eric Bantock wrote:
[color=blue]
> 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[/color]
suggestion 1
create the other constant arrays. Then assign each value in a for loop
or with memcopy();
suggestion 2
create all the possible arrays, then use a pointer to choose the correct
one. | | | | re: assigning values to an array after declaring it
In <2us0d0p3ulkrhofbj1tejl5vqajgl3fp74@4ax.com> Mark McIntyre <markmcintyre@spamcop.net> writes:
[color=blue]
>On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
><eb@example.com> wrote:
>[color=green]
>>Very basic question I'm afraid. Once an array has been declared, is there
>>a less tedious way of assigning values to its members[/color]
>(than assigning values to members)
>
>No.[/color]
When Mark McIntyre says "no", it is usually safe to assume that the answer
is "yes". And vice versa.
[color=blue]
>Well, you could memcpy data into it, if you could be sure of byte order and[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=blue]
>alignment constraints.[/color]
^^^^^^^^^^^^^^^^^^^^^
An idiotic statement, as one could expect from Mark. Since the
initialiser is compiled by the same compiler, there are no representation
issues at all. And memcpy() has NO alignment constraints: it is
actually the right way of handling assignments when the source is not
guaranteed to be aligned for its type (but this is a non-issue here,
anyway).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de | | | | re: assigning values to an array after declaring it
Eric Sosman <Eric.Sosman@sun.com> wrote:[color=blue]
> Eric Bantock wrote:[color=green]
>> 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:[/color][/color]
[snip][color=blue]
> You can use a loop:[/color]
[color=blue]
> for (i = 0; i < N; ++i)
> myarray[i] = some_expression_probably_involving_i;[/color]
[color=blue]
> Of course, this approach requires that there is some method
> behind the apparent madness of 8,3,4,...[/color]
yes, and in this case unfortunately there is none.
[color=blue]
> const SomeType data_sets[][N] = {
> { 8,3,4, ... },
> { 6,6,8, ... },
> { 4,9,2, ... },
> ...
> };
> SomeType myarray[N];
> ...
> int k = index_of_chosen_data_set;
> memcpy(myarray, data_sets[k], sizeof myarray);[/color]
Thanks Eric (and Dan and others who suggested a similar approach) - this
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?
Eric | | | | re: assigning values to an array after declaring it
Peter Ammon <peter_ammon@rocketmail.com> wrote:[color=blue]
> Eric Bantock wrote:[/color]
[color=blue][color=green]
>> 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:[/color][/color]
[snip][color=blue]
> Initializers can be determined at runtime. Why can't you initialize
> where you define it? You can use an inner scope.[/color]
[color=blue]
> void foo(void) {
> int a, b, c;
> scanf("%d %d %d", &a, &b, &c);
> {
> float arr[] = {a, b, c};
> /* do something with arr */
> }
> }[/color]
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. | | | | re: assigning values to an array after declaring it
In <ll9Ac.293021$Ar.22857@twister01.bloor.is.net.cabl e.rogers.com> "nobody" <nobody@nowhere.non> writes:
[color=blue]
>"Mark McIntyre" <markmcintyre@spamcop.net> wrote in message
>news:2us0d0p3ulkrhofbj1tejl5vqajgl3fp74@4ax.com.. .[color=green]
>> On Wed, 16 Jun 2004 15:55:26 +0000 (UTC), in comp.lang.c , Eric Bantock
>> <eb@example.com> wrote:
>>[color=darkred]
>> >Very basic question I'm afraid. Once an array has been declared, is there
>> >a less tedious way of assigning values to its members[/color]
>> (than assigning values to members)
>>
>> No.
>>
>> Well, you could memcpy data into it, if you could be sure of byte order[/color]
>and[color=green]
>> alignment constraints.
>>[/color]
>But that wouldn't be *assigning* (as in "assigning values to its members"),
>would it?[/color]
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: Dan.Pop@ifh.de | | | | re: assigning values to an array after declaring it
In <cas5s6$31n$1@us23.unix.fas.harvard.edu> Eric Bantock <eb@example.com> writes:
[color=blue]
>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?[/color]
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: Dan.Pop@ifh.de | | | | re: assigning values to an array after declaring it
On Wed, 16 Jun 2004 17:01:07 -0700, Peter Ammon
<peter_ammon@rocketmail.com> wrote:
[color=blue]
>Eric Bantock wrote:
>[color=green]
>> 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...
>>[/color]
>Initializers can be determined at runtime. Why can't you initialize[/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=blue]
>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 */
> }
>}[/color]
This is C99 only, correct?
--
aib
ISP e-mail accounts are good for receiving spam. | | | | re: assigning values to an array after declaring it
On Tue, 22 Jun 2004 03:51:44 GMT, Orhan Kavrakoglu
<galga.met@ttnet.net.tr> wrote:
[color=blue]
> On Wed, 16 Jun 2004 17:01:07 -0700, Peter Ammon
> <peter_ammon@rocketmail.com> wrote:[/color]
[color=blue][color=green]
> >Initializers can be determined at runtime. Why can't you initialize[/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=green]
> >where you define it? You can use an inner scope.[/color][/color]
[color=blue][color=green]
> > {
> > float arr[] = {a, b, c};
> > /* do something with arr */
> > }
> >}[/color]
>
> This is C99 only, correct?[/color]
For an aggregate (list) initializer, yes; in C89 all aggregate
initializer values must be constant=compile-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.net |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|