Thread: Using a string to point to a string??

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    4

    Using a string to point to a string??

    Working on a program in C, where I am trying to replace some code to make it more generic. The code is
    Code:
         x2 = Arial30_a_Size[0];
         y2 = Arial30_a_Size[1];
         c = &Arial30_a;
    



    Arial30_a_Size is [code]const int Arial30_a_Size[]={23,40};[\code]
    and Arial30_a is a much larger array
    that starts like this
    Code:
    const unsigned short Arial30_a[] ={249,0x00,8,0xFF, 14,0x00,12,0xFF,. . . . .
    Arial30 is a font, for which each character of the font has the two arrays as above.

    To make the code easier to change between different fonts, I have tried the following:
    Code:
    char Font1[] ={"Arial30"};  //name of one font
    char Font2[] = {"Arial20"}; //name of another font
    char local1[30]={0x00}, local2[30]={0x00},local3[20]={0x00};
    //create 3 strings to build the need pointers
    //now cat together each string
         strcat(local1,Font1);
         strcat(&local1,"_a_Size[0]");
         strcat(local2,Font1);
         strcat(&local2,"_a_Size[1]");
         strcat(&local3,"&");
         strcat(local3,Font1);
         strcat(&local3,"_a");
     //local1 now has "
    Arial30_a_Size[0]" //local2 now has "
    Arial30_a_Size[1]" //local3 now has "
    &Arial30_a" //NOW COMES THE PROBLEM! I need to assign the value held by //
    &Arial30_a to c,
    Arial30_a_Size[0] to x2, and
    Arial30_a_Size[1] to //y2 and I cannot figure out how. I have tried * and &, but only get //the value of the first letter in the string
    x2 = local1; y2 = local1; c = local3;

    Any help would be appreciated,
    Kind regards,
    David

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you simplify this so that you only show what you're trying to do for a single thing, not for three things at once. So, let's take this as an example:
    Code:
    char Font1[] = "Arial30";
    char local3[20] = "";
    strcat(local3, "&");
    strcat(local3, Font1);
    strcat(local3, "_a");
    Notice that:
    • I omitted the braces around "Arial30".
    • I wrote "" since you were trying to initialise the arrays to hold empty strings.
    • I did not take the address of local3 when passing it as an argument to strcat: local3, being an array, will be converted to a pointer to its first element, to you should not take its address unless you really want a pointer to the entire array of char rather than a pointer to char.

    Next, from what I understand, you "need to assign the value held by &Arial30_a to c". What is c? You didn't show the declaration of c. The thing is, local3 stores the string "&Arial30_a". This string doesn't hold a value (other than itself, I guess). I suspect that you might be thinking of the "variable variables" feature that some programming languages C, but that is not a C language feature. If you elaborated on what is c and what is &Arial30_a, maybe we could suggest an appropriate alternative (a balanced binary tree map? a hash table?)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    4

    Using a string to point to a string??

    int *c;
    x2 and y2 are just int's
    Thanks for your response
    David


    Quote Originally Posted by laserlight View Post
    I suggest that you simplify this so that you only show what you're trying to do for a single thing, not for three things at once. So, let's take this as an example:
    Code:
    char Font1[] = "Arial30";
    char local3[20] = "";
    strcat(local3, "&");
    strcat(local3, Font1);
    strcat(local3, "_a");
    Notice that:
    • I omitted the braces around "Arial30".
    • I wrote "" since you were trying to initialise the arrays to hold empty strings.
    • I did not take the address of local3 when passing it as an argument to strcat: local3, being an array, will be converted to a pointer to its first element, to you should not take its address unless you really want a pointer to the entire array of char rather than a pointer to char.

    Next, from what I understand, you "need to assign the value held by &Arial30_a to c". What is c? You didn't show the declaration of c. The thing is, local3 stores the string "&Arial30_a". This string doesn't hold a value (other than itself, I guess). I suspect that you might be thinking of the "variable variables" feature that some programming languages C, but that is not a C language feature. If you elaborated on what is c and what is &Arial30_a, maybe we could suggest an appropriate alternative (a balanced binary tree map? a hash table?)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is Arial30_a?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    4
    Arial_a is an array that I use to display the character a in an Arial 30 font. Contents are
    Code:
    const int Arial30_a_Size[]={23,40};    //size of the character in pixels (23 wide, 40 high)
    const unsigned short Arial30_a[] ={    //the first 249 bits of the character are the background color, then 8 bits are white, the next 114 bits are the background color . . . . .
    249,0x00,8,0xFF, 14,0x00,12,0xFF, 11,0x00,14,0xFF, 9,0x00,5,0xFF, 5,0x00,6,0xFF, 7,0x00,
    5,0xFF, 8,0x00,4,0xFF, 7,0x00,4,0xFF, 9,0x00,4,0xFF, 20,0x00,4,0xFF, 18,0x00,6,0xFF, 
    13,0x00,11,0xFF, 10,0x00,14,0xFF, 9,0x00,10,0xFF, 1,0x00,4,0xFF, 8,0x00,6,0xFF, 6,0x00,
    4,0xFF, 7,0x00,5,0xFF, 8,0x00,4,0xFF, 7,0x00,4,0xFF, 9,0x00,4,0xFF, 7,0x00,4,0xFF, 
    9,0x00,4,0xFF, 7,0x00,4,0xFF, 8,0x00,5,0xFF, 7,0x00,4,0xFF, 7,0x00,6,0xFF, 7,0x00,
    5,0xFF, 5,0x00,7,0xFF, 8,0x00,16,0xFF, 9,0x00,9,0xFF, 2,0x00,4,0xFF, 11,0x00,5,0xFF, 
    5,0x00,4,0xFF, 217,0x00, 254};                                                                                       //254 is the terminator of the character
    


  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, reading your initial post again, it sounds like this is chosen at compile time, not run time. I think a simpler approach is to associate say, an enum with the fonts such that each font has a unique constant that corresponds to an array index. Then, create a font struct to hold all these font data for a given font. You then create font objects and store them in this font array. To use a font would then involve something like font_set[ARIAL30], where font_set[ARIAL30].data would correspond to what you now call Arial30_a, and font_set[ARIAL30].size would correspond to what you now call Arial30_a_Size. Likewise for font_set[ARIAL20].

    EDIT:
    So for your "make the code easier to change between different fonts" use case, you would do something like:
    Code:
    const enum FontID chosen_font = ARIAL30;
    Then use font_set[chosen_font] everywhere else such that you just change one line and recompile to use a different font.
    Last edited by laserlight; 03-10-2019 at 11:15 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    4
    Thanks for the suggestion of how to make this work - especially the EDIT - that makes more sense. I appreciate your time in helping me.
    Kind regards,
    David

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixed point from string
    By Golf7 in forum C Programming
    Replies: 7
    Last Post: 10-09-2013, 01:07 PM
  2. String and Floating Point Conversion Help
    By dfghjk in forum C++ Programming
    Replies: 14
    Last Post: 05-04-2008, 12:11 PM
  3. How to get a part of a string from a point onwards
    By pseudonoma in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 04:09 PM
  4. convert a floating point number to string
    By heeroyung in forum C Programming
    Replies: 1
    Last Post: 10-02-2005, 01:03 AM
  5. floating point to string
    By Sathyabodh in forum C Programming
    Replies: 2
    Last Post: 08-13-2003, 02:58 AM

Tags for this Thread