【问题标题】:C : Generating structure variable name from inputted numberC:从输入的数字生成结构变量名称
【发布时间】:2019-11-07 11:14:00
【问题描述】:

有没有什么方法可以把一个字母和一个数字连接在一起,变成一个结构变量名?我试图让我的程序为每个员工生成一个单独的结构变量,并将结构变量名称作为他们的 ID 号。

   struct employee {
    int idnum;
    char name[];
    float salary;
}

int main(){
    //get employee id
    int id;
    printf("enter id number: ");
    scanf("%d", &id);
    //makes it into a structure variable
    struct employee /*'e' + id no.*/;
}

谢谢

【问题讨论】:

  • 不,C 没有动态变量名。使用数组并将id 设为数组索引。
  • 这有什么意义?其余代码如何知道生成的变量名称是什么?
  • 为什么要在变量名中包含ID号?只需使用struct employee emp;,其余代码使用emp
  • @Barmar 好吧,我计划将每个员工的数据存储在自己的结构变量中,所以如果员工想获取他的信息,稍后,他所要做的就是输入他的身份证号码,然后自动检索它。我希望这是有道理的:)
  • 如何在 ID 之后命名变量?只需在所有员工结构中搜索emp.idnum == id

标签: c


【解决方案1】:

不。您可以做的最好的事情是将 id 映射到特定结构实例的某种方式。数组是处理少量数据的简单方法。哈希图是一种更通用的方法。

【讨论】:

    【解决方案2】:

    不,您不能在程序运行时即时生成任何变量名。

    为此,如果最大 id 不是那么大,请使用数组。

    struct employee e[MAXID+1];
    

    如果最大id很大,需要实现一个员工id到数组索引的转换器。

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多