Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

# Static vs dynamic arrays

Part of ArraysCategory

## Description

Shows the use of static and dynamic arrays, when references are copied and duplication is needed.

## Example

```import std.c.stdio;
import std.string;

int main(char[][] args)
{
int[] a = new int;
static int a1 = [10, 20, 30, 40];
int b, c;
int[] d, e;

// initialize the vector a with the values from a1 (i = 0..2, j = a1 .. a1)
foreach (int i, int j; a1)
a[i] = j / 10;
printf("a1 = %d\n", a1);
// copy the vector a1 in the vector b
b[] = a1;
printf("\tb = %d\n", b);
// duplicate the vector a1 in the vector b (not needed for static arrays)
c[] = a1.dup;
printf("\tc = %d\n", c);
printf("a = %d\n", a);
// copy the vector a in the vector c (d is dynamic, so only the reference is copied)
d = a[];
printf("\td = %d\n", d);
// duplicate the vector a in the vector c (in this case it makes difference)
e = a[].dup;
printf("\te = %d\n", e);
// now a1 is modified
a1 = 0;

printf("a1 = %d\n", a1);
// b is not modified
printf("\tb = %d\n", b);
// neither is c
printf("\tc = %d\n", c);
// now a is modified
a = 0;

printf("a = %d\n", a);
// d IS modified, because it is just a reference
printf("\td = %d\n", d);
// e IS NOT modified, because it is a copy
printf("\te = %d\n", e);
return 0;
}
```

## Source

 Link http://www.dsource.org/tutorials/index.php?show_example=149 Posted by Anonymous
{"ft_lang_label":"__label__en","ft_lang_prob":0.5305628,"math_prob":0.99548835,"size":1433,"snap":"2021-21-2021-25","text_gpt3_token_len":512,"char_repetition_ratio":0.22323303,"word_repetition_ratio":0.08097166,"special_character_ratio":0.43056524,"punctuation_ratio":0.18518518,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9984092,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-12T23:53:42Z\",\"WARC-Record-ID\":\"<urn:uuid:e7201151-6d94-401f-93a7-1d66abbadb19>\",\"Content-Length\":\"13621\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d9db0795-d020-4930-aaa5-4d2154d89682>\",\"WARC-Concurrent-To\":\"<urn:uuid:53e7b385-d506-42ae-9458-a94b20907589>\",\"WARC-IP-Address\":\"95.216.24.116\",\"WARC-Target-URI\":\"https://dsource.org/projects/tutorials/wiki/StaticVsDynamicArraysExample\",\"WARC-Payload-Digest\":\"sha1:2ERG75WGOJ33F3XRGGZ43RCI5X6WF6WX\",\"WARC-Block-Digest\":\"sha1:GOGM5FJVHWP5XEOE4VWAYFSYFUY5XOR5\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487586465.3_warc_CC-MAIN-20210612222407-20210613012407-00054.warc.gz\"}"}