Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>WhozCraig's crystal ball says your problem is in <code>array_create()</code>. Specifically, the field <code>arr</code> is not allocated properly. Your <code>array_create()</code> should look something like this:</p> <pre><code>array_t array_create(int length) { array_t res = {length, NULL}; if (length &gt; 0) { res.arr = malloc(length * sizeof(*res.arr)); if(res.arr == NULL) { printf("malloc(%d) failed\n", length * sizeof(*res.arr)); exit(1); } } return res; } </code></pre> <p>You also need to fix those empty returns. Maybe you just want to print a message and exit.</p> <hr> <p>Update Mon Nov 18 16:13:48 CST 2013</p> <p>This is a whole different version that allocates an array_t from the heap. The error handling is kind of ugly ( all those places that return 0; ). This should work, the other worked too, but maybe an array_t on the heap is what you are looking for.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; typedef struct { size_t count; int *arr; } array_t; array_t *array_create(size_t count){ array_t *newArr = malloc(sizeof(array_t)); if(newArr == 0) return 0; newArr-&gt;count = count; newArr-&gt;arr = calloc(sizeof(int),count); if(newArr-&gt;arr == 0) return 0; return newArr; } array_t *array_get(FILE* file){ size_t count; array_t *arr; int i = 0; if (!file) return 0; if (!fscanf(file, "%zu", &amp;count)) return 0; arr = array_create(count); if(arr == 0) return 0; for (i = 0; i &lt; arr-&gt;count; i++){ if (!fscanf(file, "%d", &amp;arr-&gt;arr[i])) return 0; } for (i = 0; i&lt;arr-&gt;count; i++) printf("%d ", arr-&gt;arr[i]); printf("\n"); return arr; } int main(void) { array_t *arr; int i = 0; arr = array_get(stdin); if(arr == 0) return -1; for (i = 0; i&lt;arr-&gt;count; i++) printf("%d ", arr-&gt;arr[i]); getch(); // getch is windows return 0; } </code></pre>
    singulars
    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.
 

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