在c中打印结构的字段(动态内存分配) [英] printing fields of structure in c (dynamic memory allocation)

查看:90
本文介绍了在c中打印结构的字段(动态内存分配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的菜鸟,请创建此程序来帮助我学习.目的是将足球运动员添加到团队中并打印信息.

I'm a noob at C and create this program to help me learn. Purpose is to add soccer players to a team and print info.

我正在尝试打印俱乐部结构的字段,但是当程序进入打印方法时,我所有的值都是垃圾或地址.如何获得真实"值

I'm trying to print fields of my club structure but when my program gets to my print method, all my values are garbage or addresses. How can I get "real" values

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 8

typedef struct player {
    int id;
    char *position;
} Player;

typedef struct club {
    int size;
    Player *team[SIZE];
} Club;


Player *create_player(int id, const char *description);
void create_team(Club *club);
void print_club(const Club *club);
void add_player_to_club(Club *club, int id, const char *position);



int main() {
    Club club;

    create_team(&club);
    add_player_to_club(&club, 1, "forward");
    add_player_to_club(&club, 2, "goalie");
    print_club(&club);

    return 0;
}

Player *create_player(int id, const char *description){

    Player *player;

    player = malloc(sizeof(Player));

    if(description == NULL){
        player->position = NULL;

    } else {
        player->position = malloc(strlen(description) + 1);
        strcpy(player->position, description);
        player->id = id;
    }
    return player;
}

void create_team(Club *team){

    team = malloc(sizeof(Club));

    if (team == NULL) {
        return;
    } else {
        team->size = 0;
    }
}
void print_club(const Club *club) {

    int i = 0;

    if (club == NULL) {
        return;
    } else if (club->size == 0) {
        printf("No team members\n");
    } else {
        for (i = 0; i < SIZE; i++) {
            printf("Id: %d Position: %s\n", club->team[i]->id,
                   club->team[i]->position);
        }
    }
}
void add_player_to_club(Club *club, int id, const char *position){


    if (club == NULL || club->size >= SIZE) {
        return;
    } else {
        Player player = *create_player(id, position);

        club->team[club->size -1] = &player;

    }
}

这是我的调试会话的照片

Here's a pic of my debugging session

调试器

推荐答案

问题1

create_teammain没有做任何有用的事情.您正在更改函数局部变量的值.结果,clubmain中保持未初始化.您将继续使用它,就像它是有效对象一样,这是导致未定义行为的原因.

create_team is not doing anything useful for main. You are changing the value of a function local variable. As a consequence club remains uninitialized in main. You proceed to use it as though it is valid object, which is cause for undefined behavior.

您可以将该功能更改为:

You can change that function to:

void create_team(Club *team){
    team->size = 0;
    for (int i = 0; i < SIZE; ++i )
    {
       team->team[i] = NULL; // Unfortunate choice of variable names
                             // but should be OK.
    }
}

问题2

您要在add_player_to_club中存储一个指向函数局部变量的指针.该指针将变为无效,函数将返回.

You are storing a pointer to a function local variable in add_player_to_club. That pointer becomes invalid the function returns.

    Player player = *create_player(id, position);
    club->team[club->size -1] = &player;  // Problem

将其更改为:

    club->team[club->size] = create_player(id, position);
    club->size++;

问题3

您也在打印print_club中的Player.团队中并不总是SIZEPlayer个.换行

You are printing too may Players in print_club. There aren't always SIZE number of Players in the team. Change the line

    for (i = 0; i < SIZE; i++) {

    for (i = 0; i < club->size; i++) {

这篇关于在c中打印结构的字段(动态内存分配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆