-9

Is it possible to create a C-function that creates automatically a given number of variables? How are variables named?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
user16720
  • 1
  • 1
  • 3
    I don't think this is possible, it sound like a job for an array. – meskobalazs Jul 31 '13 at 08:36
  • I suppose you could do something with a macro, and use another macro to reference the generated variables. Though the question, of course, is why and what!? – Joe Jul 31 '13 at 08:38
  • No, but you can create an array dynamically of given number size and use `array[index]` as variable. – Grijesh Chauhan Jul 31 '13 at 08:39

4 Answers4

3

Variables are an artifact of your source code. During runtime (which is when your function actually executes) there is only memory and registers. Maybe you want an array of a certain length?

Joey
  • 344,408
  • 85
  • 689
  • 683
2

The solution is to use array. example:

//n is number of variables

int *var;

var=  malloc(sizeof(int) * n);

variables are named var[0], var[1]....var[n-1]
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Suriya Chaudary
  • 136
  • 1
  • 5
0

If by "variables" you mean "global variables outside the scope of the function", and by "create" you mean "declare and define", then NO.

Bogdan Alexandru
  • 5,394
  • 6
  • 34
  • 54
0

Do you mean like the register_globals 'feature' in PHP? Thank goodness, no.

Pranav Negandhi
  • 1,594
  • 1
  • 8
  • 17