0

I'm not exactly sure what to name this question, but I am working on a gui in Visual Studio 2015, using MFC c++.

I have a structure that looks like this

static dataCollect::StatSrcListL_t s_struct9[] =
{
    //  Name                 Addr                                   Type                        Mask  Stride  User    
    { "Rx PMA Reset Done",   statsRW::statMode9egrDeltDat,  dataCollect::Irg,    statMode9egrDelt_rxPmaRstDoneMask, 0x000 },
    { "Rx Reset Done",       statsRW::statMode9egrDeltDat,  dataCollect::Irg,    statMode9egrDelt_rxResetDoneMask,  0x0000 },
    { "Rx Buffer Underflow", statsRW::statMode9egrDeltDat,  dataCollect::IrgErr, statMode9egrDelt_rxBufUflwMask,    0x0000 },
    { "Rx Buffer AE",        statsRW::statMode9egrDeltDat,  dataCollect::IrgErr, statMode9egrDelt_rxBufAeMask,      0x0000 },
    { "Rx Buffer AF",        statsRW::statMode9egrDeltDat,  dataCollect::IrgErr, statMode9egrDelt_rxBufAfMask,      0x0000 },
    { "Rx Buffer Overflow",  statsRW::statMode9egrDeltDat,  dataCollect::IrgErr, statMode9egrDelt_rxBufOflwMask,    0x0000 },
    { "PRBS Err Det",        statsRW::statMode9egrDeltDat,  dataCollect::IrgErr, statMode9egrDelt_prbsErrMask,      0x0000 },
    { "PRBS Lck Det",        statsRW::statMode9egrDeltDat,  dataCollect::Irg,    statMode9egrDelt_prbsLckMask,      0x0000 },
    { "Tx Reset Done",       statsRW::statMode9ingDeltDat,  dataCollect::Irg,    statMode9ingDelt_txRstDoneMask,    0x000 },
    { "Tx PMA Reset Done",   statsRW::statMode9ingDeltDat,  dataCollect::Irg,    statMode9ingDelt_txPmaRstDoneMask, 0x000 },
    { "Tx Buf OFlow/Uflow",  statsRW::statMode9ingDeltDat,  dataCollect::IrgErr, statMode9ingDelt_txBufOfUfMask,    0x0000 },
    { "Pwr Good",            statsRW::statMode9ingDeltDat,  dataCollect::Irg,    statMode9ingDelt_pwrGoodMask,      0x000 },
    { "QPLL Lock Status",    statsRW::statMode9ingDeltDat,  dataCollect::Irg, 1 << statMode9ingDelt_qpllLock,       0x000 }
};

I have been trying to play around with different ways of making statMode9egrDeltDat have the number assigned dynamically. Depending on the project being used with it could range from about 10 to 50 of these being made and writing that individually 50 times in the code seems like a bit of a waste of time and space. Everything is the same except the number 9 (in this example) would start at 0, 1, 2, 3 so on and so on.

So my question is, how would I be able to put the above code into a loop to increment the number to a specified number.

Liam P
  • 217
  • 5
  • 13
  • [convert string to a variable name] (http://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type) please refer to this link. It is not possible in c++ to use value of a string as a variable name, which is what you are trying to do make various strings with diff numbers and then use those strings as variable names in the struct – Ravi Jun 23 '16 at 04:09
  • Presumably there's some good reason for the redundancy. Well then. Wrap that table in a class, because class constructors are good at initializing things. – Cheers and hth. - Alf Jun 23 '16 at 04:24
  • You could use a map instead. – Ahmed Akhtar Jun 23 '16 at 04:51
  • What problem are you trying to solve? I couldn't see one. Except the issue with your premature optimization approach. Have you identified a performance issue, and traced it back to the piece of code you presented in your question? I'm pretty sure there isn't. – IInspectable Jun 23 '16 at 11:06
  • @IInspectable The problem I am hoping to solve is to take all of the number 9's from above and make it able to be incremented in a loop of some sort. That way i can avoid having it repeated as many times as needed for the program. – Liam P Jun 23 '16 at 12:49
  • I had someone suggest I try it using `memcpy` but I don't even know where to begin using that method – Liam P Jun 23 '16 at 12:52
  • Had you considered picking up an introductory book on C++? It doesn't appear that you understand variable names. And that flawed mental model lead to the question you asked (that really doesn't make much sense). – IInspectable Jun 23 '16 at 12:59
  • I am currently taking weekend classes to learn c++. A book would definitely be a good addition though. I wanted to push my skills a little bit but i just can't figure this one out. And I'm not sure how i would use a map instead as @Ahmed pointed out. – Liam P Jun 23 '16 at 13:23
  • Variable names are strictly a compile-time construct. Once the compiler is done, they are history. At runtime, there are no variable names any more. This should make it immediately obvious, that you cannot implement a loop that creates the array (that, btw., has static storage duration, so it must exist before your application even starts to run). You could implement some preprocessor hacks, but I would suggest getting some books from [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329) first. – IInspectable Jun 23 '16 at 13:33
  • Thanks i will certainly be looking into picking one of those books up. – Liam P Jun 23 '16 at 13:37

2 Answers2

0

It looks like you are trying to create tokens for your array dynamically. As others have mentioned in the comments it may be a better design to use a class to initialize your array, or use another data structure such as std::map.

However this may help you: take a look at the ## (token-pasting) operator. This is a preprocessor operator for turning arguments into tokens. Here is an example of it's usage.

Example program

#include <iostream>

#define makevar(n) prefix ## n ## suffix

int main()
{
  auto makevar(1) = 1;

  std::cout << prefix1suffix << std::endl;
  return 0;
}

Output

1

Zev Isert
  • 915
  • 11
  • 20
  • Thank you for the answer! This kind of works for me but i am a little unsure as to how to make it dynamic. I don't understand how to make `prefix1suffix` increment in a loop, something like `prefix(i)suffix` where `i` increases from 0 to 9. Either that or I'm missing something very important... – Liam P Jun 23 '16 at 19:26
  • You're not missing much, since the value of `i` isn't determined until runtime, as iinspectable has mentioned. The macro will always expand to `prefixisuffix`. Turns out this doesn't help you much. Perhaps try using a `std::map`? – Zev Isert Jun 23 '16 at 20:44
  • It a project from work and when i go to use `std::map` i just get the message `'map': is not a member of 'std'`. So i dont think that is an option i can use. I am almost thinking it might not be possible in my case – Liam P Jun 24 '16 at 12:56
  • Did you include `` and ``? – Zev Isert Jun 27 '16 at 15:59
0

I was able to work out a partial solution. It doesn't quite do what i need 100% but it is enough to continue work as of right now.

static dataCollect::StatSrcListL_t s_struct[10][(sizeof(s_struct9) / sizeof(dataCollect::StatSrcListLedL_t))];

for (int i = 0; i < NumTables; i++) {

    (dataCollect::StatSrcListL_t*) memcpy(&s_struct[i], s_struct9, sizeof(s_struct9));

    for (int j = 0; j < (sizeof(s_struct9) / sizeof(dataCollect::StatSrcListL_t)); j++)
    {
        s_struct[i][j].address = s_struct[i][j].address + offset;
    }
    offset   += (statsRW::statMode9egrDeltDat - statsRW::statMode8egrDeltDat);

}
Liam P
  • 217
  • 5
  • 13