 <p>you're creating temporary variable <code>a</code> in function <code>calculateFlexibility</code>, then you store pointer to <code>f.flex</code> variable, but after function is ended - <code>a</code> is gone from memory, so your <code>f.flex</code> pointer is now pointing to nowhere</p> <p>if you want to have really variable length, you should do something like this:</p> <pre><code>Flexibility calculateFlexibility() { Flexibility f; f.flex = (int*)malloc(....); return f; } </code></pre> <p>and at the end of program:</p> <pre><code>free(f.flex); </code></pre> <p>for proper arguments of malloc I suggest you to read: <a href="http://en.cppreference.com/w/c/memory/malloc" rel="nofollow">http://en.cppreference.com/w/c/memory/malloc</a></p>