www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - array function

reply "Namal" <sotis22 mail.ru> writes:
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);

}
Aug 31 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
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.
Aug 31 2015
parent reply "Namal" <sotis22 mail.ru> writes:
On Monday, 31 August 2015 at 11:27:20 UTC, Rikki Cattermole wrote:

 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.
Hmm, this has never been a problem for me in C++ #include <vector> std::vector<int> foo(int N){ std::vector<int> V(N); int some_array[N]; std::vector<int> other_V; return other_V; } int main(){ std::vector<int> V = foo(12); } compiles
Aug 31 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Namal:

 std::vector<int> foo(int N){

 	std::vector<int> V(N);
 	int some_array[N];
VLAs are not present in D. Bye, bearophile
Aug 31 2015
parent reply "Namal" <sotis22 mail.ru> writes:
On Monday, 31 August 2015 at 12:00:26 UTC, bearophile wrote:
 Namal:

 std::vector<int> foo(int N){

 	std::vector<int> V(N);
 	int some_array[N];
VLAs are not present in D. Bye, bearophile
Yah, I guess I have been damaged with them when I started to learn programming in C++ >(
Aug 31 2015
parent reply "Namal" <sotis22 mail.ru> writes:
Hey guys, since I am learning D arrays here, can you tell me the 
best way to remove an element at the end of an array or at some 
index i?
Aug 31 2015
parent reply "cym13" <cpicard openmailbox.orgk> writes:
On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
 Hey guys, since I am learning D arrays here, can you tell me 
 the best way to remove an element at the end of an array or at 
 some index i?
import std.algorithm; T[] arr; arr = arr.remove(index); http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays
Aug 31 2015
parent "cym13" <cpicard openmailbox.org> writes:
On Monday, 31 August 2015 at 13:17:30 UTC, cym13 wrote:
 On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:
 Hey guys, since I am learning D arrays here, can you tell me 
 the best way to remove an element at the end of an array or at 
 some index i?
import std.algorithm; T[] arr; arr = arr.remove(index); http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays
Also, to remove the last element you can use slices: T[] arr; arr = arr[0..$-1];
Aug 31 2015