Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

assigning array...

whats wrong?... all i need is to assign a value to my "myCh" variable?.... ( im a beginners )... pls modify my code

main() {
  int myCh[5];

  myCh = getChar( 1 );
}

getChar( int x ) {

   switch( x ) {
      case 1:
         myCh = {1,2,3,4,5};   // is this possible?
         break;
      case 2:
         myCh = {5,4,3,2,1};
   }

   return myCh;

}
0
athan
Asked:
athan
  • 4
  • 4
  • 3
  • +5
1 Solution
 
athanAuthor Commented:
how can i assign an array value? it works if i initialize it but i dont want to coz i have different value to put on it.

int myCh[5] = {1,2,3,4,5};   //  this one is OK; no errors

but i dont want to initialize the value first.  I want to assign in running mode.
0
 
athanAuthor Commented:
actually im using Turbo C... theres no topic about Turbo C thats why I just post here on C++
0
 
AxterCommented:
You need to use brackets to point to a specific item in the array.
myCh[0] = getChar(1);
myCh[1] = getChar(1);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AxterCommented:
>>myCh = {1,2,3,4,5};   // is this possible?

No.  You can't do that.
0
 
AxterCommented:
If you're trying to fill the entire array, you should pass it to your function.
getChar(1, &myCh);

Change the prototype in your function to match what you want to pass to it.
0
 
athanAuthor Commented:
my problem is the length of my variable actually is 20... it seems that my code is quite long if i used braces

// i dont like this one
myCh[0] = 1;
myCh[1] = 2;
    .
    .
    .
myCh[20] = 20;

// why this one is not possible? it works on javascript;
myCh[20] = { 0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 };

any suggestion on how to minimize my code?



0
 
madfaceCommented:
well in c++ and i think C you can do the following

char myCh[] = {1, 2, 3, 4, 5};

but thats in c++, I have no clue in turbo C

or you can iterate through the array and add values
its only like 2 lines of code

for(int num = 0; num < sizeOfYourArrayGoesHere; num++)
    {
    myCh[num] = num;
    //or what ever you want to do here
    }
0
 
athanAuthor Commented:

any suggestion on this function?

getChar( int x ) {

   int myCh[5];
 
  switch( x ) {
     case 1:
        myCh[5] = {9,5,3,7,8};   // this doesnt work; why?
        break;
     case 2:
        myCh[0] = 9;       //   this one works but i dont like it coz
              myCh[1] = 5;      // i have 20 indexes :(
              myCh[2] = 3;
              myCh[3] = 7;
              myCh[4] = 8;
  }

  return myCh;

}
0
 
madfaceCommented:
you could do this

if(x == 1)
{
    int tempCh[] = {1,1,1,2,2,3,4,4,4};
    memcpy(myCh, tempCh, sizeof(tempCh));
}

but there could be a better way im just not sure what it is

oh ya you cant do this in a switch block(at least not with my compiler anyway) cause you cant declare new vars in a case block
0
 
gvgCommented:
char tempCh1[] = {1,2,3,4,5}
char tempCh2[] = {5,4,3,2,1}

switch( x )
{
 case 1 : memcpy( myCh, tempCh1, sizeof( tempCh1 ) ) : break;
 case 2 : memcpy( myCh, tempCh2, sizeof( tempCh2 ) ) : break;
}

If you how ever have a string instead of values you can do it like this.

switch( x )
{
  case 1 : memcpy( myCh, "12345", 5 ) : break;
  case 2 : memcpy( myCh, "54321", 5 ) : break;
}



0
 
havman56Commented:
so simple



void getChar( int x ) {

 int myCh[5];

 switch( x ) {
    case 1:
     {
      int  myCh[5] = {9,5,3,7,8};   // this doesnt work; y?

       break;
     }
    case 2:
        myCh[0] = 9;       //   this one works but i dont like it coz
             myCh[1] = 5;      // i have 20 indexes :(
             myCh[2] = 3;
             myCh[3] = 7;
             myCh[4] = 8;
 }



 }
0
 
havman56Commented:
here declare the variable and initialise inside the switch case block.

let me analsyse ur code.

myCh[5] = {9,5,3,7,8};   // this doesnt work; why?

see u initialsies the array {9,5,3,7,8} in the way claled compile time initialisation.

the compiler states " if any compile time initialisation should be done before any executable statement in a block."

in ur code ur initislaising after executing a statemnt switch () so not valid.

now i have created a block and initialsied it.





0
 
BlackDiamondCommented:
athan,
The code below will do what you want.  This is fine if you never plan to change the values.  If the values are going to change, then you should read them from a file or another source. You could then size and populate the myCh array based on that information.

madface/qvq,
The examples you gave with sizeof would never work.  sizeof(tempCH) is the equivalent of sizeof(int *) which will always return 4 on a 32 bit machine (4 bytes) or 8 on a 64 bit machine.

-----

#DEFINE MYCHLEN 5

main() {
 int myCh[MYCHLEN];

 getChar( 1, myCh );
}

void getChar( int x , int * myCh) {

  int temp1[] = {1,2,3,4,5};
  int temp2[] = {5,4,3,2,1};

  switch( x ) {
     case 1:
        memcpy(myCh, temp1, MYCHLEN * sizeof(int));
        break;
     case 2:
        memcpy(myCh, temp2, MYCHLEN * sizeof(int));
        break;
     default:
        break;
  }

}
0
 
BlackDiamondCommented:
By the way, since this is a C++ topic area, I should mention that there are better ways to do this in C++ (the example is ANSI C).  If you would like a real C++ example, let us know.
0
 
havman56Commented:
black diamond see the question why we cant do array initialsiition here ?
not u r own interpretation of the question

myCh[5] = {9,5,3,7,8};   // this doesnt work; why?
is aquestion.

my commenst is
case 1:
    {
     int  myCh[5] = {9,5,3,7,8};   // this will work

      break;
    }
will work .

there may eb lot of ways to do .. i do agree with black diamond





0
 
BlackDiamondCommented:
haveman,
You are correct that your code example will initialize the mcCh arrray.  The problem is that you will have a scoping issue (myCH will no longer exist when you exit the switch statement).

So there are really 2 problems that you are dealing with here, scope and initialization.

You can only use the bracket type initialization in a  declaration.  After that, you have to address the array variable just like any other pointer.
0
 
havman56Commented:
see the question
it is not related with anything of scoping issue
he wants the solution why it is not working ?

i feel i had given some reasonable comments that it!!

.
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20116520.html
http://www.experts-exchange.com/questions/Q.20191031.html
http://www.experts-exchange.com/questions/Q.20230981.html
http://www.experts-exchange.com/questions/Q.20258372.html
http://www.experts-exchange.com/questions/Q.20260577.html
http://www.experts-exchange.com/questions/Q.20274179.html




PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
griesshCommented:
Moondancer

I suggest to accept "Axter" comment as an answer.

======
Werner
0
 
MoondancerCommented:
Thank you, Werner, this has been done.
Moondancer - EE Moderator
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 4
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now