Home > Back-end >  How to structure allocates memory and assignment?
How to structure allocates memory and assignment?

Time:04-10

#include
#include

Struct chenjibiao
{
Char id [10];
Char name [30].
Int chenji;
};
Int main ()
{
Int I, n;
The scanf (" % d ", & amp; n);
//the number of the students in the given
Struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
for(i=0; I{
The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
}
}
Example
3
1234567890 liming 99
5362152436 wangwu 100
4523658967 zhangsan 67
To achieve this should be how to change? Just started to learn C language, please show just a little big,

CodePudding user response:

Struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
Change
Struct chenjibiao * p=(struct) students * malloc (sizeof (struct students) * n);

The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, (p + I) - & gt; Chenji);

CodePudding user response:

reference 1st floor qybao response:
struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
Change
Struct chenjibiao * p=(struct) students * malloc (sizeof (struct students) * n);

The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, (p + I) - & gt; Chenji);

Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, & amp; ((p + I) - & gt; Chenji));

CodePudding user response:

 
#include
#include

Struct chenjibiao
{
Char id [10];
Char name [30].
Int chenji;
};

Int main ()
{
Int I, n;
The scanf (" % d % * c ", & amp; n);
//the number of the students in the given
Chenjibiao students [3].

For (I=0; I & lt; n; + + I)
The scanf (" % d % % s % s * c ", & amp; Students [I]. Id, & amp; Students [I]. Name, & amp; Students [I]. Chenji);
For (int j=0; J & lt; n; + + j)
Printf (" % d % s % s \ n ", students [j]. Journal of id, students [j]. Name, students [j]. Chenji);
return 0;
}


Words to structure dynamically allocated memory to allocate and to release the trouble is dead
Directly in a local variable is bad

CodePudding user response:

The
reference 3 floor KarivV response:
 
#include
#include

Struct chenjibiao
{
Char id [10];
Char name [30].
Int chenji;
};

Int main ()
{
Int I, n;
The scanf (" % d % * c ", & amp; n);
//the number of the students in the given
Chenjibiao students [3].

For (I=0; I & lt; n; + + I)
The scanf (" % d % % s % s * c ", & amp; Students [I]. Id, & amp; Students [I]. Name, & amp; Students [I]. Chenji);
For (int j=0; J & lt; n; + + j)
Printf (" % d % s % s \ n ", students [j]. Journal of id, students [j]. Name, students [j]. Chenji);
return 0;
}


Words to structure dynamically allocated memory to allocate and to release the trouble is dead
With local variables directly bad


Each has advantages, dynamic allocation of space can also be used in other functions, local variable can not be,

CodePudding user response:

reference 1st floor qybao response:
struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
Change
Struct chenjibiao * p=(struct) students * malloc (sizeof (struct students) * n);

The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, (p + I) - & gt; Chenji);

Thank you (* °?=3 °)

CodePudding user response:

refer to the second floor mozhju response:
Quote: refer to 1st floor qybao response:

Struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
Change
Struct chenjibiao * p=(struct) students * malloc (sizeof (struct students) * n);

The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, (p + I) - & gt; Chenji);

Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, & amp; ((p + I) - & gt; Chenji));

Thank you (* °?=3 °)

CodePudding user response:

refer to the second floor mozhju response:
Quote: refer to 1st floor qybao response:

Struct chenjibiao students;
Struct * p=& amp; Students;
P=(struct) students * malloc (sizeof (struct students) * n);
Change
Struct chenjibiao * p=(struct) students * malloc (sizeof (struct students) * n);

The scanf (" % d % s % s ", & amp; P - & gt; Id, & amp; P - & gt; The name, & amp; P - & gt; Chenji);
Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, (p + I) - & gt; Chenji);

Change
The scanf (" % d % s % s ", (p + I) - & gt; Id (p + I) - & gt; The name, & amp; ((p + I) - & gt; Chenji)); nullnullnullnull
  • Related