0

I am trying to create some 2d arrays according to user's input, and naming them with index value in a for loop. For example, if the user's input is 4, we create 4 2d arrays which names are "array1, array2, array3, array4". The reason why it's a 2d arrays because some string values are stored in them.

  int main(int argc, char const *argv[]){
    for (int i=0; i<4; i++){
      char Cards + i [4][20]; //something like this?
    }
  return 0;
  }
5
  • 3
    You can't have dynamic variable names in C. Use another array dimension. Commented Mar 30, 2022 at 14:47
  • 2
    Not possible as stated. All identifiers (such as variable names) used in a C program must be established at compile time. Commented Mar 30, 2022 at 14:48
  • 1
    You can not declare variables like this. What you can do is add them to another array, effectively making it a 3D array and accessing them by using someArray[index] Commented Mar 30, 2022 at 14:49
  • 2
    Make one 3d array, like char Cards[99][4]20] so that Cards[i] now addresses a [4][20] 2d array. Commented Mar 30, 2022 at 14:49
  • Thank you guys, I would just store something in the first element to differentiate Commented Mar 30, 2022 at 14:53

1 Answer 1

1

This is a common beginner misunderstanding. Variable names have absolutely no relation to user I/O nor do they exist in the linked executable. Names exists solely for the benefit of the programmer and nobody else. Therefore translating user input to variable names doesn't make any sense.

It rather sounds as if you should have a (possibly multi-dimensional) array with name meaningful to the programmer. You wouldn't name your dog "Dog1 because it's my first dog", now would you? Preferably name the array based on what you plan to store inside it. Then perhaps resize it depending on user input.

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

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.