digitalmars.D.learn - array function
- Namal (16/16) Aug 31 2015 Hello,
- Rikki Cattermole (12/28) Aug 31 2015 You cannot define static arrays using runtime information.
- Namal (14/25) Aug 31 2015 Hmm, this has never been a problem for me in C++
- bearophile (4/7) Aug 31 2015 VLAs are not present in D.
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
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
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
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
On Monday, 31 August 2015 at 12:00:26 UTC, bearophile wrote:Namal:Yah, I guess I have been damaged with them when I started to learn programming in C++ >(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
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
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
On Monday, 31 August 2015 at 13:17:30 UTC, cym13 wrote:On Monday, 31 August 2015 at 13:00:49 UTC, Namal wrote:Also, to remove the last element you can use slices: T[] arr; arr = arr[0..$-1];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