Solved

# assigning array...

Posted on 2002-03-06
Medium Priority
322 Views
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
Question by:athan
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4
• 3
• +5

Author Comment

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

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

Axter earned 800 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

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

No.  You can't do that.
0

LVL 30

Expert Comment

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

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

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

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

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

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

LVL 4

Expert Comment

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

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

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.

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

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

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

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

ID: 6863762
see the question
it is not related with anything of scoping issue
he wants the solution why it is not working ?

.
0

LVL 1

Expert Comment

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

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

ID: 6955282
Moondancer

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

======
Werner
0

LVL 1

Expert Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
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â€¦
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month14 days, 16 hours left to enroll