Call by (char) or (string)?

This is a discussion on Call by (char) or (string)? within the C++ Programming forums, part of the General Programming Boards category; I'm have a small loop to save a few lines of code. It cycles through some characters: T1[], T2[], T3[], ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    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+[]);
    Last edited by Blackroot; 01-04-2006 at 06:56 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    You mean you have say a string
    char foo[] = "cos";

    And somehow you want to call the cos function in math.h ?

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    -,- 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?

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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?

  5. #5
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    your explanation is hopeless!!!
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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;
    Is that what you're asking?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    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?

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    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!
    Last edited by Blackroot; 01-04-2006 at 05:56 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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.
    Last edited by hk_mp5kpdw; 01-04-2006 at 06:04 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    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 -,-
    Last edited by Blackroot; 01-04-2006 at 07:23 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    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.

    Tyvm for all your help!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 03:29 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21