Solved

assigning array...

Posted on 2002-03-06
20
286 Views
Last Modified: 2010-04-01
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
Comment
Question by:athan
  • 4
  • 4
  • 3
  • +5
20 Comments
 

Author Comment

by:athan
ID: 6846042
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
 

Author Comment

by:athan
ID: 6846107
actually im using Turbo C... theres no topic about Turbo C thats why I just post here on C++
0
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 6846132
You need to use brackets to point to a specific item in the array.
myCh[0] = getChar(1);
myCh[1] = getChar(1);
0
 
LVL 30

Expert Comment

by:Axter
ID: 6846137
>>myCh = {1,2,3,4,5};   // is this possible?

No.  You can't do that.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6846140
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
 

Author Comment

by:athan
ID: 6846144
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
 
LVL 1

Expert Comment

by:madface
ID: 6846160
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
 

Author Comment

by:athan
ID: 6846183

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
 
LVL 1

Expert Comment

by:madface
ID: 6846270
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
 
LVL 1

Expert Comment

by:gvg
ID: 6846842
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Expert Comment

by:havman56
ID: 6846870
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
 
LVL 4

Expert Comment

by:havman56
ID: 6846882
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
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6848899
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
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6848939
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
 
LVL 4

Expert Comment

by:havman56
ID: 6849905
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
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6851097
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
 
LVL 4

Expert Comment

by:havman56
ID: 6863762
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6905928
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
 
LVL 11

Expert Comment

by:griessh
ID: 6955282
Moondancer

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

======
Werner
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6955375
Thank you, Werner, this has been done.
Moondancer - EE Moderator
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now