|
|
View previous topic :: View next topic |
Author |
Message |
Tonwo Guest
|
Stupid question about arrays |
Posted: Thu Apr 26, 2007 4:05 am |
|
|
I have this problem when I work with arrays,I want to create an array, and later I want to give values.
I know I can do this:
int a[2]={3,6};
but why I cant do this?
int a[2]
.
.
.
a={3,6}; |
|
|
frequentguest Guest
|
|
Posted: Thu Apr 26, 2007 7:25 am |
|
|
because it's not correct C syntax...
[code]
a[0]=3;
a[1]=6;
[/code]
is the most simple way to do it, but there are dozens of ways of performing this behavior. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Apr 26, 2007 8:21 am |
|
|
You need to give exact instructions on where you want each value to be stored. An array is a group of numbers that have a common reference but each location is considered to be an individual variable. Simply stating
a = {3,6};
doesn't say where you want each one to be placed. If you had an array
a[100]
and you wanted a value of 100 placed in location a[90] you would need to specify that it be placed there. You're wanting the program to 'assume' you want the numbers placed at the lowest location, in order. You can't do this. You need to specify, exactly, what you want to do with no assumptions allowed. You need to write:
a[90] = 100;
As with your example, you can declare:
int a[2];
Then stuff values into it:
a[0] = 3;
a[1] = 6;
That's just the way it is.
Ronald |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|