将变量数增加 1 [英] Increase a variable number by 1

查看:41
本文介绍了将变量数增加 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有大量字符都声明为比另一个高 1.例如m1、m2、m3...

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...

有什么方法可以将我在 for 循环中搜索的数字增加 1?我有一长串字母,我需要检查它们是否与个人匹配,但由于情况限制,我无法使用字符串.

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 是我要查找的特定字符,m1 是我必须作为个人存储的一长串字符中的第一个

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

我的尝试无法运行:

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

不幸的是,由于我的应用程序的限制,我只能在我的解决方案中使用 stdio.h 和 stdlib.h.任何帮助将不胜感激

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

推荐答案

变量名由编译器使用,但不是生成的可执行文件的一部分,因此在运行时无法访问.您可以通过使用各个变量的地址初始化的数组来模拟类似的事情:

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);
    }
}

输出:

a0:0
a1:10
a2:15

这篇关于将变量数增加 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆