开发者

Howto make a var. name with a var. in C/C++?

开发者 https://www.devze.com 2023-01-21 06:04 出处:网络
I have some vars live: int foo1; int foo2; .. and I want to reach them from: for (int i = 1;i<=2;i++) { // howto get foo1 and foo2?

I have some vars live:

int foo1;
int foo2;
..

and I want to reach them from:

for (int i = 1;i<=2;i++)
{
   // howto get foo1 and foo2?
}

how to get them?

E开发者_运维百科DIT, end what when it will be no int but a Opject *pointer;?


You can't; you need an array of some kind. e.g.:

int foo[2];   /* Two elements, foo[0] and foo[1] */
for (int i = 0; i < 2; i++)
{
    foo[i] = i;
}

or:

int foo1;
int foo2;
int *p[] = { &foo1, &foo2 };   /* Array of pointers */
for (int i = 0; i < 2; i++)
{
    *p[i] = i;   /* Changes foo1 or foo2 */
}

I don't fully understand your last question, but if you mean that you want the data types to be Object * rather than int, then all you need to do is substitute Object * for int into my code examples above (other than for int i = 0, obviously).

0

精彩评论

暂无评论...
验证码 换一张
取 消