Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use dynamic memory allocation to store runtime arrays
    primarykey
    data
    text
    <p>The following is a problem which i encountered while writing a program where i prompt a user to input as many values as i want, and then i print the results for each integer thus entered.</p> <p>I was suggested to use dynamic memeory allocation by <a href="https://stackoverflow.com/users/319824/gcc">https://stackoverflow.com/users/319824/gcc</a> in my last question <a href="https://stackoverflow.com/questions/11536306/how-to-test-my-program-against-an-input-test-case-file/11536412#11536412">How to test my program against an input test case file</a>.</p> <hr> <p>My code is as follows:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct node { int data; struct node *next; }*start; void insertatend(int d) { struct node *n; n=(struct node *)malloc(sizeof(struct node)); n-&gt;data=d; n-&gt;next=NULL; if(start==NULL) { start=n; } else { struct node *tmp; for(tmp=start;tmp-&gt;next!=NULL;tmp=tmp-&gt;next); tmp-&gt;next=n; } } int max(int a,int b) { int c=(a&gt;b)?a:b; return c; } int maxCoins(int n) { int arr[n+1],i; arr[0]=0; arr[1]=1; arr[2]=2; arr[3]=3; if(n&gt;2) { for(i=3;i&lt;=n;i++) { int k= arr[(int)(i/2)]+arr[(int)(i/3)]+arr[(int)(i/4)]; arr[i]=max(i,k); } } return arr[n]; } int main(void) { int coins,i; start=NULL; struct node*p; while(scanf("%d",&amp;coins)) { insertatend(coins); } for(p=start;p!=NULL;p=p-&gt;next) { printf("%d\n",p-&gt;data); } getchar(); return 0; } </code></pre> <hr> <p>The program runs successfully. I am able to give any number of inputs, and if I try to break out by pressing CTRL + Z ,the program does not respond. Can't I use dynamic memory allocation for problems like these ? And if yes, where am i wrong ? </p>
    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.
 

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