Home > Back-end > In the structure of dynamic array input problem
In the structure of dynamic array input problem
Time:02-15
#include #include # define max_size 100 Typedef struct { Int length; Int * arr; } sqlist; Int initlist_sq (sqlist num) { Num. Arr=(int *) malloc (sizeof (int) * (max_size)); if(! Num. Arr) The exit (EXIT_FAILURE); Num. Length=0; return 0; } Int main () { Sqlist num. Int I, n, flag; Flag=initlist_sq (num); If (flag) printf (" allocation failed \ n ");//determine whether allocate memory success The else printf (" successful \ n "); The scanf (" % d ", & amp; Num. Length); For (I=0; iThe scanf (" % d ", & amp; Num. Arr [I]); return 0; }
Suppose we input length of 5, why to the for loop is program stopped the scanf there? Can't input, I remember after allocating memory can be to look at it as an array,
CodePudding user response:
Have you of the memory leak
CodePudding user response:
No problem, just waiting for you to input, according to a few times more enter
CodePudding user response:
Modified as follows, for your reference:
# include & lt; stdio.h> #include # define max_size 100 Typedef struct { Int length; Int * arr; } sqlist;