添加结构体以动态改变内存空间量
该代码的数据结构必须是它的编写方式。我在打印和添加到内存位置时遇到问题。我认为我的添加功能可以正常工作,但我不确定。我认为我没有正确移动内存来打印和添加。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pointer.h"
/********************************************
Creates more memory for size (strut * rec+1)
*********************************************/
employee *create(int record){
employee *new_employee = malloc(sizeof(employee) * (record+1));
return new_employee;
}
/********************************************
Copies the data from one structure to a new structure with
size "structure" multipled by rec+1
***********************************************/
employee *copy(employee *data, int record){
employee *new_employee = create(record);
memcpy(new_employee, data, record * sizeof(employee));
/*
int i;
for(i = 0; i<record;i++){
new_employee->first = data->first;
new_employee->last = data->last;
new_employee->start_date = data->start_date;
new_employee->sal = data->sal;
data++;
}
*/
/********************
Needs to free the old struct
*********************/
//deleteData(data, record);
return new_employee;
}
/********************************************
Function prints everything in the struct
*********************************************/
void printStruct(employee *data, int record){
employee *head = data;
employee *employeeDB = head;
int i;
for(i = 0; i<record; i++){
printf("\nEntry: %d\n", i+1);
printf("The employee's name is %s %s\n", employeeDB->first, employeeDB->last);
printf("The employee was hired on: %s\n", employeeDB->start_date);
printf("The employee make $%f\n\n", employeeDB->sal);
employeeDB++;
}
}
/******************************************
Function frees the old data base
*******************************************/
void deleteData(employee *data, int record){
int i;
for(i = 0; i<record; i++){
free(data->first);
free(data->last);
free(data->start_date);
data++;
}
free(data);
}
/******************************************
Adds an employee to the new structure
*******************************************/
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record){
fprintf(stderr, "\n\nEmployee data = %s for record # %d\n\n", data->first, record);
employee *head = create(record);
employee *employeeDB = head;
/*
employeeDB = copy(data, record);
fprintf(stderr, "Copied %d records\n", record);
*/
//
//
//
// BUILD NEW EMPLOYEE
employee * new_employee = malloc(sizeof(employee));
new_employee->first = fname;
new_employee->last = lname;
new_employee->start_date = date;
new_employee->sal = salary;
// COPY EXISING DATA TO EMPLOYEEDB
memmove(employeeDB,data,sizeof(employee));;
fprintf(stderr, "Now the first record has first = %s\n", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %s\n", head->first);
// MOVE HEAD
employeeDB = employeeDB + record;
memmove(employeeDB, new_employee, sizeof(employee));
fprintf(stderr, "Now the first record has first = %s\n", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %s\n", head->first);
//
//
//
//
return head;
}
/**************************
Starts of the main function
***************************/
int main(void){
//Keeps track of the number of records that are in the structure
int rec = 0;
//Keeps the number of accesses to the structure. Even with the one entry the structure has not been accessed.
int acc = 0;
//Holds the input information for the menu
int input;
//holds the information for inputing user first name
char fname[MAXSIZE];
//holds the information for inputing user last name
char lname[MAXSIZE];
//holds the information for for the startdate
char start[MAXSIZE];
//holds the information for the salary;
float sal;
/*********************************
This next section adds an employee to the record
************************************/
//This creates the first entry to the dynamic structure.
employee *data_base = create(rec);
data_base->first = "FIRST";
data_base->last = "LAST";
data_base->start_date = "June-20th-2006";
data_base->sal = 55555.55;
//increase the number of records
rec = rec+1;
/**********************************
/*
This starts the while loop that creates the menu for the user to choose from
**********************************/
while(1){
printf("\nWELCOME TO myEMPLOYEE DATABASE\n");
printf("\n------------------\n");
printf("\nMENU");
printf("\n1. Print All Records\n");
printf("2. Print Number of Records\n");
printf("3. Print Size of database\n");
printf("4. Add an Employee\n");
printf("5. Delete all Employee\n");
printf("6. Print number of accesses to database\n");
printf("7. EXIT\n");
printf("\nENTER SELECTION: ");
/* This scanf functions scans for the appropiate input, if the input is
not one of the selection items the program exits*/
scanf("%d", &input);
switch (input) {
case 1:
printStruct(data_base, rec);
acc = acc + 1;
break;
case 2:
printf("The number of records are %d\n", rec);
acc = acc + 1;
break;
case 3:
printf("The size of the data base is %d\n", sizeof(employee) * rec);
acc = acc + 1;
break;
case 4:
printf("\nPlease enter the employee's first name:");
scanf("%s", fname);
printf("\nPlease enter the employee's last name:");
scanf("%s", lname);
printf("\nPlease enter the employee's start date (Month-Day-Year):");
scanf("%s", start);
printf("\nPlease enther the employee's salaray:");
scanf("%f", &sal);
acc = acc + 1;
break;
case 5:
break;
case 6:
printf("The number of accesses to the data is: %d", acc);
break;
case 7:
printf("The program has exited");
return 1;
default:
// If a wrong selection is entered it will display this message.
printf("Please enter a selection 1-7");
}
}
}
/*
Header file for pointer.c
*/
#include <string.h>
#define MAXSIZE 255;
typedef struct info {
char *first;
char *last;
char *start_date;
float sal;
}employee;
employee *create(int record);
employee *copy(employee *data, int record);
void printStruct(employee *data, int record);
void deleteData(employee *data, int record);
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record);
The data structure of this code has to be how it has been written. I am having issues printing and adding to the memory locations. I think that I have the add function working properly but I am not sure. I don't think I am moving in memory properly to print and add.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pointer.h"
/********************************************
Creates more memory for size (strut * rec+1)
*********************************************/
employee *create(int record){
employee *new_employee = malloc(sizeof(employee) * (record+1));
return new_employee;
}
/********************************************
Copies the data from one structure to a new structure with
size "structure" multipled by rec+1
***********************************************/
employee *copy(employee *data, int record){
employee *new_employee = create(record);
memcpy(new_employee, data, record * sizeof(employee));
/*
int i;
for(i = 0; i<record;i++){
new_employee->first = data->first;
new_employee->last = data->last;
new_employee->start_date = data->start_date;
new_employee->sal = data->sal;
data++;
}
*/
/********************
Needs to free the old struct
*********************/
//deleteData(data, record);
return new_employee;
}
/********************************************
Function prints everything in the struct
*********************************************/
void printStruct(employee *data, int record){
employee *head = data;
employee *employeeDB = head;
int i;
for(i = 0; i<record; i++){
printf("\nEntry: %d\n", i+1);
printf("The employee's name is %s %s\n", employeeDB->first, employeeDB->last);
printf("The employee was hired on: %s\n", employeeDB->start_date);
printf("The employee make $%f\n\n", employeeDB->sal);
employeeDB++;
}
}
/******************************************
Function frees the old data base
*******************************************/
void deleteData(employee *data, int record){
int i;
for(i = 0; i<record; i++){
free(data->first);
free(data->last);
free(data->start_date);
data++;
}
free(data);
}
/******************************************
Adds an employee to the new structure
*******************************************/
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record){
fprintf(stderr, "\n\nEmployee data = %s for record # %d\n\n", data->first, record);
employee *head = create(record);
employee *employeeDB = head;
/*
employeeDB = copy(data, record);
fprintf(stderr, "Copied %d records\n", record);
*/
//
//
//
// BUILD NEW EMPLOYEE
employee * new_employee = malloc(sizeof(employee));
new_employee->first = fname;
new_employee->last = lname;
new_employee->start_date = date;
new_employee->sal = salary;
// COPY EXISING DATA TO EMPLOYEEDB
memmove(employeeDB,data,sizeof(employee));;
fprintf(stderr, "Now the first record has first = %s\n", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %s\n", head->first);
// MOVE HEAD
employeeDB = employeeDB + record;
memmove(employeeDB, new_employee, sizeof(employee));
fprintf(stderr, "Now the first record has first = %s\n", employeeDB->first);
fprintf(stderr, "Now the first record(head) has first = %s\n", head->first);
//
//
//
//
return head;
}
/**************************
Starts of the main function
***************************/
int main(void){
//Keeps track of the number of records that are in the structure
int rec = 0;
//Keeps the number of accesses to the structure. Even with the one entry the structure has not been accessed.
int acc = 0;
//Holds the input information for the menu
int input;
//holds the information for inputing user first name
char fname[MAXSIZE];
//holds the information for inputing user last name
char lname[MAXSIZE];
//holds the information for for the startdate
char start[MAXSIZE];
//holds the information for the salary;
float sal;
/*********************************
This next section adds an employee to the record
************************************/
//This creates the first entry to the dynamic structure.
employee *data_base = create(rec);
data_base->first = "FIRST";
data_base->last = "LAST";
data_base->start_date = "June-20th-2006";
data_base->sal = 55555.55;
//increase the number of records
rec = rec+1;
/**********************************
/*
This starts the while loop that creates the menu for the user to choose from
**********************************/
while(1){
printf("\nWELCOME TO myEMPLOYEE DATABASE\n");
printf("\n------------------\n");
printf("\nMENU");
printf("\n1. Print All Records\n");
printf("2. Print Number of Records\n");
printf("3. Print Size of database\n");
printf("4. Add an Employee\n");
printf("5. Delete all Employee\n");
printf("6. Print number of accesses to database\n");
printf("7. EXIT\n");
printf("\nENTER SELECTION: ");
/* This scanf functions scans for the appropiate input, if the input is
not one of the selection items the program exits*/
scanf("%d", &input);
switch (input) {
case 1:
printStruct(data_base, rec);
acc = acc + 1;
break;
case 2:
printf("The number of records are %d\n", rec);
acc = acc + 1;
break;
case 3:
printf("The size of the data base is %d\n", sizeof(employee) * rec);
acc = acc + 1;
break;
case 4:
printf("\nPlease enter the employee's first name:");
scanf("%s", fname);
printf("\nPlease enter the employee's last name:");
scanf("%s", lname);
printf("\nPlease enter the employee's start date (Month-Day-Year):");
scanf("%s", start);
printf("\nPlease enther the employee's salaray:");
scanf("%f", &sal);
acc = acc + 1;
break;
case 5:
break;
case 6:
printf("The number of accesses to the data is: %d", acc);
break;
case 7:
printf("The program has exited");
return 1;
default:
// If a wrong selection is entered it will display this message.
printf("Please enter a selection 1-7");
}
}
}
/*
Header file for pointer.c
*/
#include <string.h>
#define MAXSIZE 255;
typedef struct info {
char *first;
char *last;
char *start_date;
float sal;
}employee;
employee *create(int record);
employee *copy(employee *data, int record);
void printStruct(employee *data, int record);
void deleteData(employee *data, int record);
employee *add(employee *data,char *fname, char *lname, char *date, float salary, int record);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除
#define MAXSIZE 255;
末尾的;
。无论您在何处使用MAXSIZE
,它都会被255;
替换,因此char fname[255;];
显然是无效的。#define MAXSIZE 255
Remove the
;
from the end of#define MAXSIZE 255;
. Wherever you haveMAXSIZE
it'll be replaced by255;
, and thereforechar fname[255;];
is obviously invalid.#define MAXSIZE 255