Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem with C code of array

Status
Not open for further replies.

london

Member level 4
Member level 4
Joined
Jun 30, 2006
Messages
79
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,945
I wrote a programing using array that
int Letter[]
....
....
switch key
{
case 1 : Letter[]={2,1,0}; break;
case 2 : Letter[]={3,2,3}; break;
}

but compiler indicate error on Letter[]={x,x,x} line. Why is that? can u correct me?
 

Re: Abt C code

The array initialization block - {.... , .... , ....} - can be used only on the same line of the array declartion, e.g:

int array[]={1,2,3}; /*correct*/

but

int array[];
array[]={1,2,3}; /*wrong*/

so you can not use the fragent you wrote

case 1 : Letter[]={2,1,0}; break;
case 2 : Letter[]={3,2,3}; break;

instead you have to assign each element of the array individually, e.g:

case 1: Letter[0]=2;
Letter[1]=1;
Letter[2]=0;
break;
case 2: ...........

or make use of a for loop if you can.

Another two comments on your code:
1-While declaring your array , you have to declare its size unless you are using the initialization block - which is {.... , ..... , ..... }
e.g

int array[3] ; /* correct */
int array[]; /* wrong */

int array[3]={1,2,3}; /*correct*/
int array[]={1,2,3}; /* also correct*/

2-Do not forget the parentheses in the switch statement , i.e

switch (expression)
{
....
....
....
}

Hope I was able to help. Good luck!
 

    london

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top