Home > Back-end >  The scanf type char * prompt error Linux
The scanf type char * prompt error Linux

Time:11-28

#include
#include
# define datesize 64

Struct students
{
Char * name;
Char * member;
Char * state;
};

Void appendmessage ()
{
Struct students * s;
Printf (" the student's name: ");
//1
tagChar * name;
The scanf (" % s ", name);
//1 end tag
//2
tagThe scanf (" % s ", (* s). The name).
//2 end tag
Printf (" student number: ");
The scanf (" % s ", (* s) member);
Printf (" student status: ");
The scanf (" % s ", (* s). The state);
Printf (" -- written to the file - ");
The FILE * fp=fopen (" studate. TXT ", "ab");
.
}
Such as: the tag 1 input error, not but mark 2 error,

CodePudding user response:

Are wild pointer, s is wild pointer, s - & gt; Name is wild pointer, so the result is undefined, period of mistakes are possible,

 # include 
#include
# define datesize 64

Struct students
{
Char * name;
Char * member;
Char * state;
};

Void appendmessage ()
{
Struct students * s;

S=(struct) students * malloc (sizeof (struct students));
if (! S)
return;

Printf (" the student's name: ");
//1
tagChar * name=(char *) malloc (sizeof (char) * 32);
if (! Name)
return;
The scanf (" % s ", name);
//1 end tag
//2
tagS - & gt; Name=(char *) malloc (sizeof (char) * 32);
if (! S - & gt; Name)
return;
The scanf (" % s ", (* s). The name).
//2 end tag
Printf (" student number: ");
S - & gt; Member=(char *) malloc (sizeof (char) * 32);
if (! S - & gt; Member)
return;
The scanf (" % s ", (* s) member);
Printf (" student status: ");
S - & gt; State=(char *) malloc (sizeof (char) * 32);
if (! S - & gt; State)
return;
The scanf (" % s ", (* s). The state);
Printf (" -- written to the file - ");
The FILE * fp=fopen (" studate. TXT ", "ab");
//...
}

For your reference ~

The above is just a, other can also use an array or a common variable ~

CodePudding user response:

Struct students
{
Char * name;
Char * member;
Char * state;
};

If the pointer memory allocations are not familiar with, or suggest use char name [10] this way
  • Related