# Call by (char) or (string)?

• 01-04-2006
Blackroot
Call by (char) or (string)?
I'm have a small loop to save a few lines of code. It cycles through some characters:

T1[], T2[], T3[], T4[]. Now, I want to call the reference like:

T[integer][]. (Not a 2d array)

So, I guess what I mean is, can you somehow call a list of characters like that, or do you have to have them in a 2d array?

If you dont get what I mean, I'm trying to call a character value thats not in a 2d array like:

Code:

char T1[]
char T2[]
int x;

cout << (T+x+[]);

• 01-04-2006
Salem
You mean you have say a string
char foo[] = "cos";

And somehow you want to call the cos function in math.h ?
• 01-04-2006
Blackroot
-,- Am I that bad at explaining things? >.>.

Let me try and give it another crack:

Ok, I'm trying to call a value as if it were a 2d array (along thoes lines). So say I have two characters A1 and A2. I want to make a loop to save time.

So
Code:

for(x=0;x < 2; x++) {
cout << A[x];
}

My problem is that A is not an array in any form (besides being of type character). So I'm basicaly trying to call a non array as if it were an array.

It may seem pointless to do it this way, but I have a data sheet arranged as

Si1
Si2
Db1
Db2

And with the idea above, all I need to add is a way to shift from calling Si# to Db#.

I dont know how to explain that any better >.>. Is that more clear?
• 01-04-2006
Darryl
if A is not an array, what is it? If it is type char, then it only has a single char, if it has more than 1 char then it IS an array.

char A* = "test"; //is an array (for our purposes) and can be used like A[x]. IS this what you mean?
• 01-04-2006
logicwonder
• 01-04-2006
hk_mp5kpdw
Maybe:

Code:

char T1[] = "Si1";
char T2[] = "Si1";
char T3[] = "Db1";
char T4[] = "Db2";

char * index[4];

index[0] = T1;
index[1] = T2;
index[2] = T3;
index[3] = T4;

for( int x = 0; x < 4; ++x )
cout << index[x] << endl;

• 01-04-2006
tomcant
I think I see what he means. Try this:
Code:

#define REF(a) (T##a)

int T1,T2;

for(int i=0;i<2;++i)
REF(i)=some_int;

Is that what you ment?
• 01-04-2006
Blackroot
Yes! Only problem now is its giving me the error
"Undefined symbol 'TZelous' in function main()"

Heres the code I'm having trouble with:

Code:

nt main() {
#define TNUM 4
#define REF(a) (T##a)
char buffer[1];
char T1[] = "Test 1...";
char T2[] = "Test 2...";
char T3[] = "Test 3...";
char T4[] = "Test 4...";
int Pyrogodlyness;
int Zelous;

clrscr();

for(;;) {
for(Zelous=1; Zelous < TNUM; Zelous++)
if(Zelous == Pyrogodlyness) {
SetColor('g');
cout << (REF(Zelous));
SetColor('o');
} else {
cout << REF(Zelous);
}

Hk I'm trying to avoid using arrays, but thats a good idea actualy!
• 01-04-2006
hk_mp5kpdw
You can't use that #define trick with a variable name, defines are processed as part of the preprocessor and the result is then compiled after the substitution is made. In the REF define, you pass Zelous which get pasted into the T##a part and becomes TZelous. This is then compiled and obviously TZelous is an undefined symbol.

I think that's what's happening, I don't use the preprocessor much myself. Correct me if I'm wrong anybody.
• 01-04-2006
Blackroot
Hmm... It seems thats whats happening.

But then How do I give it the variables value rather than the name? Do I need to use a pointer? Or will a pointer even chage anything?

(did you mean "you cant use that define trick with a variable name" as in cant pass a variable name, or cant call a variable with that trick?)

If its impossible please let me know anytime -,-
• 01-04-2006
hk_mp5kpdw
Quote:

Originally Posted by Blackroot
did you mean "you cant use that define trick with a variable name" as in cant pass a variable name, or cant call a variable with that trick?)

I meant you can't pass a variable name. To use that and have it work, you have to pass in actual values (1, 2, 3, 4... etc) and not a variable. Like this:

Code:

#define REF(a) (T##a)

...

char T1[] = "Hello";

cout << REF(1) << endl; // Outputs "Hello"

You can't use a variable name because the preprocessor gets there first and makes a literal substitution prior to the code getting compiled. This...
Code:

int main() {
#define TNUM 4
#define REF(a) (T##a)
char buffer[1];
char T1[] = "Test 1...";
char T2[] = "Test 2...";
char T3[] = "Test 3...";
char T4[] = "Test 4...";
int Pyrogodlyness;
int Zelous;

clrscr();

for(;;) {
for(Zelous=1; Zelous < TNUM; Zelous++)
if(Zelous == Pyrogodlyness) {
SetColor('g');
cout << (REF(Zelous));
SetColor('o');
} else {
cout << REF(Zelous);
}

isn't what gets compiled. The preprocessor has a go at it first and makes the substitutions and creates this...
Code:

nt main() {
char buffer[1];
char T1[] = "Test 1...";
char T2[] = "Test 2...";
char T3[] = "Test 3...";
char T4[] = "Test 4...";
int Pyrogodlyness;
int Zelous;

clrscr();

for(;;) {
for(Zelous=1; Zelous < 4; Zelous++)
if(Zelous == Pyrogodlyness) {
SetColor('g');
cout << (TZelous);
SetColor('o');
} else {
cout << TZelous;
}

...which is then sent to the compiler to do its job, which it does and immediately reports TZelous as an unknown identifier (which it correctly is).
• 01-04-2006
Blackroot
Hmmm... Then how do I pass the variables value?

Anyways, I just turned it into an array and it couldent work better ^,^. I guess I was just being stubborn :P.