如果我有以下3个结构:
/*Structures*/
typedef struct team
{
char* name;
int playedGames;
int score;
}team;
typedef struct matchday
{
char* date;
team team1;
team team2;
team winner;
bool isPlayed;
}matchday;
typedef struct sportSeason
{
matchday *calendar;
team *ranking;
int totalMatches;
int matchesPlayed;
int remainingMatches;
}sportSeason;
和以下代码:
sportSeason *ptr = malloc(sizeof(sportSeason));
如何在结构中编辑/访问团队或比赛日数组的成员?
例如
ptr->ranking[0]->name = "Team A";
不起作用。
亲切的问候,
Aggony阿喀琉斯
完整代码(价值):
#define _CRTDDBG_MAP_ALLOC
#define MAX_BUFFER_SIZE 81
#include <stdlib.h>
#include <crtdbg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/*Structures*/
typedef struct team
{
char* name;
int playedGames;
int score;
}team;
typedef struct matchday
{
char* date;
team team1;
team team2;
team winner;
bool isPlayed;
}matchday;
typedef struct sportSeason
{
matchday *calendar;
team *ranking;
int totalMatches;
int matchesPlayed;
int remainingMatches;
}sportSeason;
/*Function prototypes*/
void initSeason(sportSeason *obj, int n);
void newTeam(team **obj, int *n);
void bufferCheck(char string[]);
int main(void)
{
char buffer[MAX_BUFFER_SIZE];
int totalMatches, teams;
sportSeason *footbalSeason = NULL;
//Memory allocation
footbalSeason = malloc(sizeof(sportSeason));
printf("Welcome to a new season: \n");
printf("Please enter the following information before registering the teams: \n");
printf("How many teams are participating: \n");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
teams = atoi(buffer);
initSeason(footbalSeason, teams);
_CrtDumpMemoryLeaks();
return 0;
}
void initSeason(sportSeason *obj, int n)
{
char buffer[MAX_BUFFER_SIZE];
sportSeason *temp = obj;
temp->totalMatches = (((n + 1) * n) / 2);
temp->matchesPlayed = 0;
temp->remainingMatches = 0;
printf("Register teams: \n");
/*Passing the adres of the pointer to the first element of the rank
array (team *) */
newTeam(&(temp->ranking), &n);
}
void newTeam(team **obj, int *n)
{
char buffer[MAX_BUFFER_SIZE];
//Allocate memory for all the teams
obj = malloc((*n) * sizeof(team));
if (obj == NULL)
{
printf("Memory reallocation failed");
}
else
{
//Add information to struct members
for (int i = 0; i < (*n); ++i)
{
obj[i]->playedGames = 0;
obj[i]->score = 0;
printf("\nEnter the team name: ");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
bufferCheck(buffer);
strcpy(obj[i]->name, buffer);
}
}
}
最佳答案
分配有问题。
当您将指针发送到指针时,您基本上会给出该指针的地址
表示您是否发送:
newTeam(&(temp->ranking), &n);
实际分配它,您需要:
在newTeam中做:
void newTeam(team **obj, int *n)
{
//blahblah
(*obj) = (team*) malloc(sizeof(team)* (*n));
为什么? a)p2p是该指针的地址,意味着实际引用我们需要的obj指针,请记住malloc返回void,这样更安全。
如果我们将其全部简化为main代码,它将类似于:
int main(){
sportSeason *ptr = (sportSeason*)malloc(sizeof(sportSeason));
ptr->totalMatches = ptr->remainingMatches = ptr->remainingMatches = 0;
ptr->calendar = (matchday*)malloc(4*sizeof(matchday));
ptr->calendar[0].date = (char*)malloc(sizeof(char) * 4);
strcpy_s(ptr->calendar[0].date,4,"hi");
printf("%s", ptr->calendar[0].date);
//...
为这些操作使用函数绝对是正确的(非常高兴您以模块化方式实现了它!
一个代码,例如将会:
int main(void)
{
char buffer[MAX_BUFFER_SIZE];
int teams;
sportSeason *footbalSeason = NULL;
//Memory allocation
footbalSeason = (sportSeason*)malloc(sizeof(sportSeason));
printf("Welcome to a new season: \n");
printf("Please enter the following information before registering the teams: \n");
printf("How many teams are participating: \n");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
teams = atoi(buffer);
initSeason(footbalSeason, teams);
...
无效initSeason(sportSeason * obj,int n)
{
字符缓冲区[MAX_BUFFER_SIZE];
sportSeason *temp = obj; //bit unnessery to have temp but w/e
temp->totalMatches = (((n + 1) * n) / 2);
temp->matchesPlayed = 0;
temp->remainingMatches = 0;
printf("Register teams: \n");
/*Passing the adres of the pointer to the first element of the rank
array (team *)<-INCORRECT you pass the address to the pointer it's basicly nothing more - you allocate it */
newTeam(&(temp->ranking), &n);
printf("%d\n",temp->ranking[0].playedGames); //will print it just fine
}
void newTeam(team **obj, int *n)
{
*obj = (team*)malloc(sizeof(team)*(*n));
obj[0]->playedGames = 0; //for eg.
}
顺便说一句,当您分配内存时,您实际上必须释放它,不要懒惰并使用_crt指令,因为它仅在调试模式下适用,并非所有编译器都支持它
因此总而言之:
要从p2p(**)访问指针(*),您需要使用:ptr并进行分配。
当您使用malloc时,请使用强制转换,因为它返回void
总是自己免费胡扯,不信任任何东西,僵尸即使在PC内存中也可以吞噬您:P
提示:我不知道您是否了解c ++,但这是一种可以帮助您的方法:
将函数视为所需数据的构造函数,因此,如果您使用纯c进行编程,则应使构建对象的函数保持最大状态,并保留破坏对象的函数,并清除所有分配并关闭文件/管道,而不执行其他操作。希望它对您有帮助,如果您愿意随时跟进,我将尽我所能,尽我所能
关于c - C如何访问另一个结构中作为结构数组一部分的结构的结构成员?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38423623/