Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 19-03-2008, 01:23 AM   #1 (permalink)
Right Off the Assembly Line
 
estranged12's Avatar
 
Join Date: Aug 2006
Posts: 28
Default array initialization


when you initialize an array, why can you only declare its elements like {1,2,3} when you initialize it and not just like x = {5,6,7}?
estranged12 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 19-03-2008, 01:39 PM   #2 (permalink)
Right Off the Assembly Line
 
Join Date: Mar 2008
Posts: 5
Default Re: array initialization

Do u mean the Array Values or the Array Index ?
aircombat is offline  
Old 19-03-2008, 02:08 PM   #3 (permalink)
In The Zone
 
vandit's Avatar
 
Join Date: May 2005
Location: If I had a webcam u could have seen me, before your eyes.
Posts: 459
Default Re: array initialization

Because after declaration the name(i.e x ) of the array points to the 1st element of the array..Hence when u write x={1,2,3} after declaration..The compiler tries 2 give the 1st element i.e. X[0] those vals. Which is not possible...Hence....
__________________
Searching for dell studio 15 ??
dell studio 15 review @ my blog

vandit007.blogspot.com

And remember the stylus that comes along with the touchscreens isnt meant for cleaning your ears !!
vandit is offline  
Old 19-03-2008, 03:02 PM   #4 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: array initialization

You can initialise it with whatever values you want.
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 19-03-2008, 03:35 PM   #5 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: array initialization

You can initialize arrays with just any element you want...

I think, you mean, array index
redhat is offline  
Old 19-03-2008, 04:38 PM   #6 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: array initialization

Um even though vandit has explained it, for the rest of you, his question was:

When we can declare-and-initialize an array as:

Code:
int x[3]={0,1,2};
Why can't one do it as:

Code:
int x[3];
x={0,1,2};
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 22-03-2008, 02:25 PM   #7 (permalink)
In The Zone
 
INS-ANI's Avatar
 
Join Date: Dec 2006
Location: Pune
Posts: 406
Default Re: array initialization

let us undertand some basic things about array.
the given array is a[n].
so when we write a[n], we are telling the machine that it is an array with n elements.
the "n" is just an number,integer.
and the variable "a" is actually a pointer to the base address of the array, that is a[0].

so int x[3];
x={1,2,3} is wrong, as it means we are trying to give some value to the variable x, though this is not a correct way either.
so the only way to add values to array while initialisation is
x[]={ ....}
or x[3]={..}
INS-ANI is offline  
Old 24-03-2008, 11:58 AM   #8 (permalink)
Pee into the Wind...
 
shady_inc's Avatar
 
Join Date: May 2007
Location: Mumbai
Posts: 782
Default Re: array initialization

You say
Code:
int a[5];
a[3]=35;
Compiler treats it as
Code:
*(a+3)=35;
So, if you declare it like
Code:
int x[3];
x={0,1,2};
compiler will treat second line as :
*(x+0)={0,1,2};
Which is obviously wrong.

P.S.: You can declare int a[3]=35; as int 3[a]=35.Both mean the same as *(a+3)==*(3+a).!
__________________
Life is as complicated as you say it is.
shady_inc is offline  
Old 24-03-2008, 12:34 PM   #9 (permalink)
In The Zone
 
anand1's Avatar
 
Join Date: Sep 2007
Location: 23 17'N, 86 05'E
Posts: 369
Default Re: array initialization

It will result into a Syntax error simple. If you were the developer than you should have added this feature in it. Any Programming language has a specific Syntax which we should follow. And this if not followed will result into a Syntax error.
__________________
Technology On The Fly
anand1 is offline  
Old 24-03-2008, 02:01 PM   #10 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: array initialization

anand1 - Yes, but behind every syntax point lies a great tale, which is what he asks.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
World's Most Complex Silicon Phased-array Chip Developed naveen_reloaded Technology News 2 05-11-2007 11:35 AM
array of JPanel in java.... tweety_bird_bunny QnA (read only) 3 27-09-2006 08:52 PM
COM initialization failed rajat22 QnA (read only) 6 30-01-2006 10:39 PM
msmsgs.exe.Dll initialization failed mahendran QnA (read only) 3 15-01-2005 10:45 AM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2