是否可以将变量作为结构成员传递?
Is it possible to pass a variable as a structure member?
如果我有这样的结构:
/* Defined structure with 3 integer as members */
typedef struct MyStruct MyStruct;
struct Myscruct{
int x;
int y;
int z;
};
int main()
{
Mystruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
printf("\nchoose witch member you want to print [ x, y or z]");
scanf("%c", &c);
while(getchar() != '\n');
printf("%d", example.c); // this is the part i don't know is possible.
return 0;
}
可以将不同的成员传递给最后一个 printf 而不是使用 if 的组合吗?
如果是这样,它的语法是什么?
不,您不能使用运行时数据来引用源代码中的符号。不过,您可以付出更多的努力来实现 特定 代码的明显目标:
typedef struct MyStruct MyStruct;
struct Myscruct{
int x;
int y;
int z;
};
int main()
{
Mystruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
int xyz;
printf("\nchoose witch membeer you want to print [ x, y or z]");
scanf("%c", &c);
switch (c) {
case 'x':
xyz = example.x;
break;
case 'y':
xyz = example.y;
break;
case 'z':
xyz = example.z;
break;
default:
puts("Oops\n");
return 1;
}
printf("%d", xyz);
return 0;
}
还有其他选择,但是 none 与您尝试的一样直接。
你要的叫反射,C没有。
一个解决方案是让 Mystruct
包含一个 数组 :
struct Mystruct {
int coordinates[3];
};
...
Mystruct example;
example.coordinates[0]=1;
example.coordinates[1]=2;
example.coordinates[2]=3;
int i;
然后向用户询问一个数字,将其存储在i
中并将其用作数组的索引:
printf("%d", example.coordinates[i]);
您可以使用 offsetof
:
#include <stdio.h>
#include <stddef.h>
typedef struct MyStruct MyStruct;
struct MyStruct{
int x;
int y;
int z;
};
int main()
{
size_t addr[] = {
offsetof(MyStruct, x),
offsetof(MyStruct, y),
offsetof(MyStruct, z),
};
MyStruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
printf("\nchoose witch membeer you want to print [ x, y or z]");
scanf("%c", &c);
printf("%d\n", *(int *)((char *)(&example) + addr[c - 'x']));
return 0;
}
输出:
choose witch membeer you want to print [ x, y or z]y
2
如果我有这样的结构:
/* Defined structure with 3 integer as members */
typedef struct MyStruct MyStruct;
struct Myscruct{
int x;
int y;
int z;
};
int main()
{
Mystruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
printf("\nchoose witch member you want to print [ x, y or z]");
scanf("%c", &c);
while(getchar() != '\n');
printf("%d", example.c); // this is the part i don't know is possible.
return 0;
}
可以将不同的成员传递给最后一个 printf 而不是使用 if 的组合吗? 如果是这样,它的语法是什么?
不,您不能使用运行时数据来引用源代码中的符号。不过,您可以付出更多的努力来实现 特定 代码的明显目标:
typedef struct MyStruct MyStruct;
struct Myscruct{
int x;
int y;
int z;
};
int main()
{
Mystruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
int xyz;
printf("\nchoose witch membeer you want to print [ x, y or z]");
scanf("%c", &c);
switch (c) {
case 'x':
xyz = example.x;
break;
case 'y':
xyz = example.y;
break;
case 'z':
xyz = example.z;
break;
default:
puts("Oops\n");
return 1;
}
printf("%d", xyz);
return 0;
}
还有其他选择,但是 none 与您尝试的一样直接。
你要的叫反射,C没有。
一个解决方案是让 Mystruct
包含一个 数组 :
struct Mystruct {
int coordinates[3];
};
...
Mystruct example;
example.coordinates[0]=1;
example.coordinates[1]=2;
example.coordinates[2]=3;
int i;
然后向用户询问一个数字,将其存储在i
中并将其用作数组的索引:
printf("%d", example.coordinates[i]);
您可以使用 offsetof
:
#include <stdio.h>
#include <stddef.h>
typedef struct MyStruct MyStruct;
struct MyStruct{
int x;
int y;
int z;
};
int main()
{
size_t addr[] = {
offsetof(MyStruct, x),
offsetof(MyStruct, y),
offsetof(MyStruct, z),
};
MyStruct example; // declared "example" with type "Mystuct"
example.x=1; // member 1 have value 1
example.y=2; // member 2 have value 2
example.z=3; // member 3 have value 3
char c;
printf("\nchoose witch membeer you want to print [ x, y or z]");
scanf("%c", &c);
printf("%d\n", *(int *)((char *)(&example) + addr[c - 'x']));
return 0;
}
输出:
choose witch membeer you want to print [ x, y or z]y
2