array function

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 31 04:27:19 PDT 2015


On 31/08/15 11:24 PM, Namal wrote:
> Hello,
>
> can someone explain to me please what I am doing wrong by passing an
> integer to this function and then just creating a static array? The
> error I get is:
>
> Error: variable N cannot be read at compile time
>
> int[] foo(int N){
>
>
>      int[N] v;
>      //do something with it
>      int[] s;
>      return s;
> }
>
> void main(){
>
>      int N = 12;
>      int[] A;
>      A=prim_numbers(N);
>
> }

You cannot define static arrays using runtime information.
You must use dynamic arrays.

int[] foo(int N) {
	int[] v;
	v.length = N;
	// do something with it
	int[] 2;
	return s;
}


Of course you can pass it in via a template argument. But this of course 
does not work at runtime like you are wanting.


More information about the Digitalmars-d-learn mailing list