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;

Int initlist_sq (sqlist & amp; Num)//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 ");
The else printf (" successful \ n ");

The scanf (" % d ", & amp; Num. Length);
For (I=0; iThe scanf (" % d ", & amp; Num. Arr [I]);

return 0;
}

CodePudding user response:

The
reference 3 floor QZJHJXJ response:
modify as follows, for reference:
 # include & lt; stdio.h> 
#include
# define max_size 100
Typedef struct
{
Int length;
Int * arr;
} sqlist;

Int initlist_sq (sqlist & amp; Num)//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 ");
The else printf (" successful \ n ");

The scanf (" % d ", & amp; Num. Length);
For (I=0; iThe scanf (" % d ", & amp; Num. Arr [I]);

return 0;
}

You just put a & amp; , then don't even passed the compilation, and we define num is not a pointer to the structure variable,

CodePudding user response:

Simple - Soft references 2 floor response:
no problem, just waiting for you to input, according to a few times more enter

No, they jump out

CodePudding user response:

reference 1/f, the truth is right or wrong response:
your this memory leak

I go online to check the memory leak is to point to my trashy free, but now this is not input

CodePudding user response:

reference 4 floor weixin_53443263 response:
Quote: reference QZJHJXJ reply: 3/f
modify as follows, for reference:
 # include & lt; stdio.h> 
#include
# define max_size 100
Typedef struct
{
Int length;
Int * arr;
} sqlist;

Int initlist_sq (sqlist & amp; Num)//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 ");
The else printf (" successful \ n ");

The scanf (" % d ", & amp; Num. Length);
For (I=0; iThe scanf (" % d ", & amp; Num. Arr [I]);

return 0;
}

You just put a & amp; Added later, don't even passed the compilation, and we define the num of also is not a pointer to the structure variable,

I'm here is to run through, didn't you say compilation problems:
The initialization int initlist_sq (sqlist num) functions in the num and the main () function in the num is two different memory space, not by address to let them into contact, light value useless ah,

CodePudding user response:

Initlist num is main inside a copy of the inside, in the function of any operation to the inside of the main num is invalid

CodePudding user response:

refer to 7th floor QZJHJXJ response:
Quote: refer to 4th floor weixin_53443263 response:
Quote: reference QZJHJXJ reply: 3/f
modify as follows, for reference:
 # include & lt; stdio.h> 
#include
# define max_size 100
Typedef struct
{
Int length;
Int * arr;
} sqlist;

Int initlist_sq (sqlist & amp; Num)//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);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related