Thread: static array is needed but size is not determinable at compile time, what do I do?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    static array is needed but size is not determinable at compile time, what do I do?

    Here's the code:
    Code:
    unsigned int* generatePopulation(const int);
    
    
    int main(){
        const int POPSIZE = 3;
        unsigned int* population;
        population = generatePopulation(POPSIZE);
        
    }
    
    
    unsigned int* generatePopulation(const int size){
        static int population[size];
        //...fill the array
        return population;
    }
    Compiler error is:
    Code:
    error: storage size of ‘population’ isn’t constant
    I've searched the error and people recommend that a variable defined with const is still a "variable", however, compiler needs a constant created with #define processor for the size of a static array, however, I can not do this, since I want to get the input from the outside(e.g. using scanf) so a preprocessor command won't help, also the array has to be static or else it will be destroyed after the function call. Is there a way for it? Thanks in advance.
    Last edited by narniat; 01-04-2019 at 11:53 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,600
    Your only option is to call malloc if your size is user-determined by reading a value using scanf.
    Code:
    void generatePopulation(unsigned int*,const int);
     
     
    int main(){
        const int POPSIZE = 3;
        unsigned int* population = malloc(POPSIZE*sizeof(*population));
        generatePopulation(population,POPSIZE); 
    }
    
    void generatePopulation(unsigned int*pop, const int size){
        //...fill the array
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Sorry, I've realized the answer but I didn't know how to delete this post.

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Salem thanks for the answer, sorry for my previous post, I sent it simultaneously with you, yes I used malloc. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static, Stack, Heap Array and time
    By 26friends26 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2011, 04:42 PM
  2. Replies: 4
    Last Post: 08-29-2010, 04:33 PM
  3. 2D array, size unknown at compile time
    By gah_ribaldi in forum C# Programming
    Replies: 10
    Last Post: 12-04-2009, 05:42 PM
  4. Array size at run time unknown
    By lechat in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2007, 12:25 PM
  5. some help needed->size of 2D array
    By creon in forum C Programming
    Replies: 13
    Last Post: 09-14-2006, 07:55 PM

Tags for this Thread