Is it possible to create a C-function that creates automatically a given number of variables? How are variables named?
            Asked
            
        
        
            Active
            
        
            Viewed 110 times
        
    -9
            
            
        - 
                    3I 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 Answers
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
- 
                    1In C, do NOT cast the return value from `malloc()`!! I mean it, don't make come over there and slap you around :-) – paxdiablo Jul 31 '13 at 08:41
- 
                    http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858 – Grijesh Chauhan Jul 31 '13 at 08:43
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
 
    