0

is there any method where you can concatenate a letter and a number together and make it into a structure variable name? I'm trying to make my program generate an individual structure variable for each employee and the struct var name being their id number.

   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.*/;
}

thanks

5
  • 1
    No, C doesn't have dynamic variable names. Use an array and make id the array index. Commented Nov 7, 2019 at 3:07
  • What would be the point of this? How would the rest of the code know what the generated variable name is? Commented Nov 7, 2019 at 3:08
  • Why do you need the variable name to include the ID number? Just use struct employee emp; and use emp for the rest of the code. Commented Nov 7, 2019 at 3:09
  • @Barmar Well, I plan to store each employee data on its own structure variable so if the employee wants to get his information, later on, all he has to do is input his id number and it automatically retrieves it. I hope this is making sense :) Commented Nov 7, 2019 at 3:19
  • And how would naming the variable after the ID help with that? Just search all the employee structures for the one with emp.idnum == id Commented Nov 7, 2019 at 3:22

2 Answers 2

1

Nope. Best you can do is some way to map an id to a particular struct instance. An array is the simple approach for small amounts of data. A hashmap is a more general way to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

No, you can't generate any variable name on the fly while the program is running.

For such purpose, use an array if the maximum id is not so large.

struct employee e[MAXID+1];

If the maximum id is very large, you need to implement a converter from employee id to array index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.