Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to initialize arrays in C: </p> <ul> <li> On the <b>stack</b> (which will handle memory for you since it will be cleaned up when your function ends)</li> <li> In the <b>heap</b> (which will require you to handle allocation and freeing on your own). </li> </ul> <p>If you would like to use the stack, you could initialize your array like this...</p> <pre><code>#define ARRAY_LENGTH 10 void *ptr; void *arr[ARRAY_LENGTH]; for (int i = 0; i &lt; ARRAY_LENGTH; i++) { arr[i] = ptr; } </code></pre> <p>You can similarly define your array in the heap as follows...</p> <pre><code>#define ARRAY_LENGTH 10 void *ptr; void **arr = malloc(sizeof(void *) * ARRAY_LENGTH); for (int i = 0; i &lt; ARRAY_LENGTH; i++) { arr[i] = ptr; } free(arr); </code></pre> <p>It is important to remember that an array (besides arrays assigned in the stack, which have some additional attributes such as length) is essentially just a pointer to the first element, and the operation <i>arr[i]</i> is the same as moving <i>i*sizeof(elem)</i> bytes away from the first element, and accessing the memory there. If you would like to get a pointer to the <i>i</i>th index in the array, then you would use notations such as...</p> <pre><code>void *indexPtr = arr + i; </code></pre> <p>or</p> <pre><code>void *indexPtr = &amp;( arr[i] ); </code></pre> <p>In this fashion, an array of <i>void*</i>'s would be of type <i>void **</i>, since the variable is a pointer to the first member of the array, which is a pointer. This can be a bit confusing, but just always try to keep in mind what type the elements of the array are, and creating a pointer to them. So if the array is of type int, then the array would be of type <i>int <em></i> or <i>int[]</i>, but if you are storing pointers to integers, you would initialize an array of type <i>int *</em></i> in either of these two forms...</p> <pre><code>int **arr = malloc(sizeof(int *) * ARRAY_LENGTH); int *arr[ARRAY_LENGTH]; </code></pre> <p>Also note that you are storing pointers, so if you run the code...</p> <pre><code>int *arr[4]; for (int i = 0; i &lt; ARRAY_LENGTH; i++) { arr[i] = &amp;i; } </code></pre> <p>Although it may seem to be that the values pointed to in the array would be as follows- [0, 1, 2, 3], but in reality it would be [4, 4, 4, 4], since what you actually have is an array of pointers all pointing to the variable <i>i</i> in your function, so whenever you change that, the values pointed to in the array will all be changed. <br> I hope this helped</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload