locked
Executing a C++ command as content of a string RRS feed

  • Question

  • I have this nested structure

    struct OutMemStream2 {
        struct A1 {
            uint8_t n1 = 0;  // pixel coordinate 
            float x1;    // pixel amplitude multiplied by Re of function
            float y1;    // pixel amplitude multiplied by Im of function
        }   a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
            ......................................
            a81, a82, a83, a84, a85, a86, a87, a88, a89, a90;
    
    } b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
    ....................................................
    b851, b852, b853, b854, b855, b856, b857, b858, b859, b860,
    b861, b862, b863, b864;            

    It is a large structure but it is still a compromise for me, eventually I will need it to be larger. In order to address variables n1, x1, y1 I tried to shape a part of a command as a content of a string. It looks something like this:

                    else
                    {
                        if (bNumber >= 100 && bNumber <= 864)
                        {
                            _itoa_s(bNumber, *buffer3, 3, 10);
                            bCharNumber = "b" + *buffer3[0] + *buffer3[1] + *buffer3[2];
                        }
                    }
                }
               (&bCharNumber).a1.n1 += 1;   <=== syntax error is here.

    The syntax error says that this expression must be a class, structure or enum. Well, it is a structure!

    The only alternative as I see now is to write a switch, and it will be a nested switch,, but it will be enormous with 864 options and 90 suboptions. Any other ideas?

    Thank you, - MyCatAlex




    • Edited by MyCatAlex Tuesday, June 2, 2020 5:11 PM
    Tuesday, June 2, 2020 2:55 PM

Answers

  • To put it simply, the compiler can't see into strings, especially not strings generated at runtime, and somehow magically convert it to a memory address/variable name.

    If you want to do this the way you are trying to then you need to dispatch it at runtime using that big switch statement that you don't want to use. This is the only option, not an alternative.

    The only other option that you have is to use arrays. This will massively simplify everything.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by MyCatAlex Tuesday, June 2, 2020 4:00 PM
    Tuesday, June 2, 2020 3:25 PM
  • struct OutMemStream2 {
        struct A1 {
            uint8_t n1 = 0;  // pixel coordinate 
            float x1;    // pixel amplitude multiplied by Re of function
            float y1;    // pixel amplitude multiplied by Im of function
        }   a[90];
    
    } b[864];

    Now you can just write  b[bNumber - 1].a[0].n1   (recall that array indexes are 0-based, not 1-based).


    Igor Tandetnik

    • Marked as answer by MyCatAlex Tuesday, June 2, 2020 7:28 PM
    Tuesday, June 2, 2020 5:40 PM

All replies

  • To put it simply, the compiler can't see into strings, especially not strings generated at runtime, and somehow magically convert it to a memory address/variable name.

    If you want to do this the way you are trying to then you need to dispatch it at runtime using that big switch statement that you don't want to use. This is the only option, not an alternative.

    The only other option that you have is to use arrays. This will massively simplify everything.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by MyCatAlex Tuesday, June 2, 2020 4:00 PM
    Tuesday, June 2, 2020 3:25 PM
  • Thank you. I don't understand how I can use the arrays. Could you sketch an example? By the way, my string is generated at runtime, but it does not come to it. It is a syntax error and it is intercepted by the compiler.

    Thank you,

    - MyCatAlex


    • Edited by MyCatAlex Tuesday, June 2, 2020 4:05 PM
    Tuesday, June 2, 2020 4:02 PM
  • struct OutMemStream2 {
        struct A1 {
            uint8_t n1 = 0;  // pixel coordinate 
            float x1;    // pixel amplitude multiplied by Re of function
            float y1;    // pixel amplitude multiplied by Im of function
        }   a[90];
    
    } b[864];

    Now you can just write  b[bNumber - 1].a[0].n1   (recall that array indexes are 0-based, not 1-based).


    Igor Tandetnik

    • Marked as answer by MyCatAlex Tuesday, June 2, 2020 7:28 PM
    Tuesday, June 2, 2020 5:40 PM
  • struct OutMemStream2 {
        struct A1 {
            uint8_t n1 = 0;  // pixel coordinate 
            float x1;    // pixel amplitude multiplied by Re of function
            float y1;    // pixel amplitude multiplied by Im of function
        }   a[90];
    
    } b[864];

    Now you can just write  b[bNumber - 1].a[0].n1   (recall that array indexes are 0-based, not 1-based).


    Igor Tandetnik

    Amazingly it compiled and it seems it is going to work. Many thanks, - MyCatAlex

    Tuesday, June 2, 2020 7:30 PM