Thread: Handling a string constant as a symbolic const name

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Handling a string constant as a symbolic const name

    I guess this is a general C question... here's my code..

    Code:
    	OnCreate(hwnd,cs,initRecArea(480,45,55,20),(HMENU)IDCE_SINGLELINE5);
    	OnCreate(hwnd,cs,initRecArea(480,105,55,20),(HMENU)IDCE_SINGLELINE7);
    	OnCreate(hwnd,cs,initRecArea(480,135,55,20),(HMENU)IDCE_SINGLELINE8);
    	OnCreate(hwnd,cs,initRecArea(480,165,55,20),(HMENU)IDCE_SINGLELINE9);
    	OnCreate(hwnd,cs,initRecArea(480,195,55,20),(HMENU)IDCE_SINGLELINE10);
    	OnCreate(hwnd,cs,initRecArea(480,225,55,20),(HMENU)IDCE_SINGLELINE11);
    	OnCreate(hwnd,cs,initRecArea(480,255,55,20),(HMENU)IDCE_SINGLELINE12);
    Since all these lines are a repetition except the second argument of initRecArea() which increments by 30, and last argument of OnCreate() which only has the last character changing, i would prefer looping this code as follows...

    Code:
            char cnt;
    
    	for (next=45, cnt=48;next<=255;next+=30, cnt++)	{
    		sprintf(buffer,"%s%d","IDCE_SINGLELINE",cnt);
    		OnCreate(hwnd,cs,initRecArea(205,45,55,20),(HMENU)buffer);
    	}
    but buffer is a string not a symbolic constant... How do i treat it as a constant? Or any other solutions to this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you can't convert strings to variables or variables to strings - since variables names are lost during the compilation - they are only there for your benefit when writing the code. Once the compiler understands what's going on, variables become memory locations and/or registers.

    You could do something like this:
    Code:
    HMENU table[] = { (HMENU)IDCE_SINGLELINE5, (HMENU)IDCE_SINGLELINE7, ... };
    ... 
            char cnt;
    
    	for (next=45, cnt=0;next<=255;next+=30, cnt++)	{
    		OnCreate(hwnd,cs,initRecArea(205,next,55,20),table[cnt]);
    	}
    A more flexible alternative would be to store the coordinates within the table as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    No, you can't convert strings to variables or variables to strings - since variables names are lost during the compilation - they are only there for your benefit when writing the code. Once the compiler understands what's going on, variables become memory locations and/or registers.

    You could do something like this:
    Code:
    HMENU table[] = { (HMENU)IDCE_SINGLELINE5, (HMENU)IDCE_SINGLELINE7, ... };
    ... 
            char cnt;
    
    	for (next=45, cnt=0;next<=255;next+=30, cnt++)	{
    		OnCreate(hwnd,cs,initRecArea(205,next,55,20),table[cnt]);
    	}
    A more flexible alternative would be to store the coordinates within the table as well.

    --
    Mats
    mhh... cool, thnx

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another trick you can do:
    Code:
            char cnt;
    
    	for (next=45, cnt=48;next<=255;next+=30, cnt++)	{
    		OnCreate(hwnd,cs,initRecArea(205,45,55,20),(HMENU)IDCE_SINGLELINE + cnt);
    	}
    If IDCE_SINGLELINEX series are numbers after each other, like
    #define IDCE_SINGLELINE45 45
    #define IDCE_SINGLELINE46 46
    #define IDCE_SINGLELINE47 47
    #define IDCE_SINGLELINE48 48
    Or the like, then the above code could work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Another trick you can do:
    Code:
            char cnt;
    
    	for (next=45, cnt=48;next<=255;next+=30, cnt++)	{
    		OnCreate(hwnd,cs,initRecArea(205,45,55,20),(HMENU)IDCE_SINGLELINE + cnt);
    	}
    Elysia, i doubt if IDCE_SINGLELINE + cnt will concatenating IDCE_SINGLELINE with the values in cnt, rather will add them together... Note also that cnt is a char, not int.. so char cnt =48 could represent the character value 0, 49 character 1, and so forth....
    Last edited by csonx_p; 05-20-2008 at 12:21 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Elysia, i doubt if IDCE_SINGLELINE + cnt will concatenating IDCE_SINGLELINE with the values in cnt, rather will add them together... Note also that cnt is a char, not int.. so char cnt =48 could represent the character value 0, 49 character 1, and so forth....
    No, what elysia intends is to have a define for the base-number, and then just add the appropriate number to it.
    Code:
    #define IDCE_SINGLELINE     0 // Use the correct number here.
    It does assume that the IDCE_SINGLELINEXX are sequential, which they may not be if they are generated by the resource editor and not added all at once.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM