我正在尝试创建一个游戏,并且当用户进入游戏时,有时会为该玩家创建一个结构。 如果玩家输入名称,游戏会提示玩家以下内容:
Type player name:> George
Choose one of the following Otions:
[+]1) to remove George from player List.
[+]0) to keep George.
玩家需要输入3个名字,并决定是否应删除或保留其中的一个或全部。
我的问题是,如果玩家决定从列表中删除一个或多个列表,我将无法保留已创建(保留)的列表。
这是程序的一部分,可以编译:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PERCENT 5
#define LEFT "left"
#define RIGHT "Right"
struct node {
int player_ID;
struct node* prev;
struct node* next;
char move_player[256];
char player_name[ 31 ];
} node;
struct node *root;
struct node *current;
struct node *player_node( int player_ID, const char *player_name );
void delete_player ( void *player_list );
int main( void )
{
char name[256] = { 0 };
int i = 0, clean;
root = player_node( PERCENT, RIGHT );
current = root;
do{
struct node *player = player_node( PERCENT, LEFT );
player->prev = current;
current->next = player;
current = player;
printf( "Type player name:> ");
if ( fgets( name, 256, stdin ) == NULL )
{
printf("Error, fgets()\n");
exit( EXIT_FAILURE );
}
name[ strcspn( name, "\n" ) ] = '\0';
strncpy( player->player_name, name, strlen( name ) );
printf( "Choose one of the following Otions:\n" );
printf( "\t[+]1) to remove %s from player List.\n", current->player_name );
printf( "\t[+]0) to keep %s.\n", current->player_name );
int opt = 0;
if ( scanf( "%d", &opt) != 1 )
{
printf("Error, scanf()\n");
}else if ( opt == 1 )
{
delete_player ( player );
}
while ( ( clean = getchar() ) != '\n' && clean != EOF );
i++;
}while( i < 3 );
struct node *tmp = root->next;
while ( tmp != NULL )
{
printf( "Name = %s", tmp->player_name );
tmp = tmp->next;
}
free( root );
}
struct node *player_node( int player_ID, const char *const movement )
{
struct node *player = malloc( sizeof( struct node ) );
player->prev = NULL;
player->next = NULL;
player->player_ID = player_ID;
strncpy( player->move_player, movement, strlen( movement) );
strncpy( player->player_name, "NULL", 5 );
return player;
}
void delete_player ( void *player_list )
{
struct node *local_client = (struct node* )player_list;
if ( local_client == current )
{
current = local_client->prev;
current->next = NULL;
} else
{
local_client->prev->next = local_client->next;
local_client->next->prev = local_client->prev;
}
free( local_client );
}
如果代码到达此地址:
struct node *tmp = root->next;
while ( tmp != NULL )
{
printf( "Name = %s", tmp->player_name );
}
列表中没有剩余的玩家,因为没有打印列表。
为什么列表为空?
答案 0 :(得分:1)
使用strcpy()
代替strncpy()
。后者比前者not safer。
更改:
strncpy(player->player_name, name, strlen( name ));
收件人:
strcpy(player->player_name, name);
对此通话执行相同操作:
strncpy( player->move_player, movement, strlen( movement) );
因为在两种情况下,您都不复制NULL终止符。
然后,当printf()
或任何标准字符串函数处理您的字符串时,它不知道何时停止...
问题的核心是[strlen()
] 3在不考虑NULL终止符的情况下计算字符串的长度:
C字符串的长度由终止的空字符确定:AC字符串的长度与字符串开头和终止的空字符之间的字符数一样(不包括终止的空字符本身)。
我也将strncpy( player->player_name, "NULL", 5 );
更改为同质。
如果必须使用strncpy()
,则只需在第三个参数中加1,如下所示:
strncpy(player->player_name, name, strlen( name ) + 1);
但是,请记住,在此代码中使用strncpy()
是毫无意义的,只会增加复杂性并降低代码的简洁度,从而只会降低可读性。