简体   繁体   English

如何将枚举变量分配给数组索引的指针

[英]How to assign enum variable to a pointer of array index

Say I have enum unit{ var1, var2, var3}; 假设我有enum unit{ var1, var2, var3}; Then I have an array in the main function 然后我在主函数中有一个数组

int main (int argc, char *argv[]);

I want to assign argv[1] to var1 , where argv[1] was input through command line, I m not sure how to go about doing this for pointer of array. 我想将argv[1]分配给var1 ,其中argv[1]是通过命令行输入的,我不确定如何为数组指针进行此操作。 I know how to get the input from command line but trouble assigning it to var1 . 我知道如何从命令行获取输入,但是很难将其分配给var1

You can't. 你不能 Enum values must be known at compile time, but argv is only known at run time. 枚举值必须在编译时已知,但argv仅在运行时已知。

You cannot change the values of enum at run-time, structs are the best implementation here, I advise you change your code to this; 您不能在运行时更改enum的值,在这里structs是最好的实现,我建议您将代码更改为此;

struct unit{ var1, var2, var3};

Then you can use a pointer to manipulate the values. 然后,您可以使用指针来操纵值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM