【发布时间】:2012-04-22 00:08:59
【问题描述】:
我需要构建一个指向动态分配结构 (DBrecord) 的指针数组,并用另一个文件的输入填充该数组。不知道如何处理。
数据文件将首先包含条目数,然后是特定顺序的条目。
numOfEntries
lastName firstName studentID year gpa expGradYear
示例:
1
Doe John 12345678 高级 3.14159 2015
这是我目前的代码:
类.h
typedef enum {firstYear, sophomore, junior, senior, grad} class;
main.c
#include <stdio.h>
#include <stdlib.h>
#include "class.h"
int main(){
//DBrecord is name for structure
struct DBrecord{
int DBrecordID; //ID for each entry, range 0-319
char *last; //student last name
char *first; //student first name
char studentID[8]; //student ID
int age; //student age
class year; //year in school
float gpa; //GPA
int expGradYear; //expected graduation year
};
int numEntries; //total number of entries, first num in data file
struct DBrecord **ptrToDB;
//scan first int in data file and assign to numEntries
scanf("%d", &numEntries);
//allocate memory for structures, each is 36 bytes
*ptrToDB = malloc (sizeof(struct DBrecord) * numEntries);
//free allocated memory
free(ptrToDB);
//build an array of pointers to dynamically allocated structures
//fill that array with input from data file
//build 7 arrays of pointers to DBrecords, one for each field except DB ID
//sort each a different way
//note the 7 arrays are pointers, no copying
//print each of the 7 sorted arrays
return 0;
}
【问题讨论】:
-
如果您要内联编辑您的原始帖子,甚至不赞成我的回答,我就出局了。
-
@KevinDTimm 冷静你的喷气机人,你还没有完全回答我的问题。一步一步来。那我就给你点赞吧。
-
您在没有评论的情况下进行了编辑 - 实现了我的 mod - 更多内容
标签: c pointers memory-management data-structures