Increase a variable number by 1

久未见 提交于 2021-02-05 08:54:49

问题


I have code in which I have a large number of characters all declared as being 1 higher than the other. e.g. m1, m2, m3...

is there any way to increase the number I'm searching for by 1 in a for loop? I have a long string of letters that I need to check to see if any of them match to the individual, but I cannot use strings due to situational limitations.

a1 is the particular character I'm looking for, m1 is the first in a long string of characters I am having to store as individuals

My attempt that wouldn't run:

for (a1 != m["%d"], &check, check++)

Unfortunately due to the limits of my application I can only use stdio.h and stdlib.h in my solution. Any help would be greatly appreciated


回答1:


Variable names are used by the compiler, but are not part of the generated executable and therefore not accessible at runtime. You can simulate something like that by an array initialized with the addresses of the respective variables:

#include <stdio.h>
int main() {
    int a0=0,a1=10,a2=15;
    int *a[3] = { &a0, &a1, &a2 };
    for (int i=0; i<3; i++) {
      int val = *(a[i]);
      printf("a%d:%d\n",i,val);
    }
}

Output:

a0:0
a1:10
a2:15


来源:https://stackoverflow.com/questions/62162475/increase-a-variable-number-by-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!