我是C的初学者,我有一个程序,我试图在函数内设置结构的值,但我不确定如何将结构传递给函数并让它修改它的原始值
我有一个简单的结构,如下所示:
struct player {
char name[40];
int acceleration;
};
只是为了玩,我这样做:
struct player terry;
terry.acceleration = 20;
strcpy(terry.name, "John Terry");
我想将此功能移至某个功能,以便我可以执行以下操作:
createPlayer(terry, 20, "John Terry");
到目前为止,我的功能看起来像这样:
void createPlayer(struct player currentPlayer, char name[], int acceleration) {
strcpy(currentPlayer.name, name);
currentPlayer.acceleration = acceleration;
}
但是当我打印出来时:
printf("The speed of %s is %d \n\n", terry.name, terry.acceleration);
我明白这一点:
The speed of is 0
我在这里做错了什么?请建议我的代码/样式的任何更改违反通常惯例。
非常感谢你!
答案 0 :(得分:3)
您需要通过指针传递结构:
将功能更改为:
void createPlayer(struct player *currentPlayer, char name[], int acceleration) {
strcpy(currentPlayer->name, name);
currentPlayer->acceleration = acceleration;
}
并称之为:
createPlayer(&terry, 20, "John Terry");
编辑:
在原始代码中,将结构传递给函数时。它是“按价值”完成的。这意味着将副本复制到函数中。因此,您在函数内对其进行的更改将在本地副本上进行,并且不会应用于原始副本。
答案 1 :(得分:1)
您可能还需要考虑“createPlayer”功能感觉好像它正在创建一个供应用程序使用的新播放器。只有当您需要在代码中使用它时才为结构分配内存,并在完成后丢弃内存,这是习惯且非常强大的。此外,这样做可以让您在运行代码时根据需要创建任意数量的“播放器”,并根据需要使用它们。
下面是一篇文章,展示如何使用动态内存分配(在执行程序时分配内存)来完成。 让我们创建一个包含我们定义的头文件(.h)
player.h:
/* this player.h header file is used by other parts of our program to access
* the player functionality definitions and interface
*/
/* the #ifndef / #define below protects against inadvertent syntax errors
* that may occur by double inclusion of the same file.
*/
#ifndef _PLAYER_H
#define _PLAYER_H
struct player {
char name[40];
int acceleration;
};
extern struct player * create_player(const char *name, int acceleration);
void delete_player(struct player *p);
const char * get_player_name(struct player *p);
void set_player_name(struct player *p, const char *name);
int get_player_accel(struct player *p);
void set_player_accel(struct player *p, int accel);
/* you can add other functions here and then define their implementation in player.c
shown below */
#endif /* _PLAYER_H */
让我们创建一个player.c,它将包含player.h中定义的函数的实现。
player.c:
/* include memory management functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "player.h" /* we created this above */
/* Define the structure */
定义管理结构分配的功能
/* create_player function allocates memory for a new player structure
* and returns the pointer to it upon succcess. It returns NULL on failure
*/
struct player * create_player(const char *name, int acceleration) {
struct player *new_player = malloc(sizeof(struct player));
/* malloc allocates memory for the sizeof the structure and returns
the pointer to it */
if ( !new_player ) {
/* typically we don't put error messages in production code
* it is handled elsewhere but verbosity
* is okay for learning, so we add an error message */
fprintf(stderr, "unable to allocate memory for a new player\n");
return NULL;
}
}
/* delete_player deallocates memory that was allocated in create_player
* only pass it the pointers that were created by create_player structure
*/
void delete_player(struct player *p) {
if (p)
free(p);
}
/* let's do some house keeping functions */
const char * get_player_name(struct player *p) {
if ( p )
return p->name;
else
return NULL;
}
void set_player_name(struct player *p, const char *name) {
if ( p ) {
strncpy(p->name, name, sizeof(p->name)); /* only 40 characters */
}
}
int get_player_accel(struct player *p) {
return p->acceleration;
}
void set_player_accel(struct player *p, int accel) {
if ( p )
p->acceleration = accel;
}
现在代码中的其他地方你可以使用上面的函数:
main.c中:
#include <stdio.h>
#include "player.h"
int main() {
struct player *player = create_player("john", 30);
if ( player ) {
printf("%s's acceleration is %d\n", player->name, player->acceleration);
}
}
使用c编译器使用两个c文件编译程序(如果使用的是C IDE,则将它们添加到项目中)。建立项目......
如果您使用命令行,则:
cc -o player_test player.c main.c
应该做的伎俩
现在我在没有使用交流编译器的情况下全部写出来,所以我可能会有一些语法错误...请从中获取C的感觉,而不是字面上使用它:)以防万一有愚蠢的错误。