设计的结构体,网友说有问题,不过找不出问题所在
这个是题目要求。这是employeeRecordT的结构。C语言不支持string 已对该部分作了修改
代码::
程序代码:
#include<stdio.h> #include<stdlib.h> typedef struct { char name[20]; char title[20]; char ssnum[20]; double salary; int witholding ; } employeeRecordT; typedef struct { int num; employeeRecordT *info; } *payrollT; payrollT Getpayroll(payrollT result) { int i = 1; result = (payrollT) malloc(sizeof(payrollT)); scanf("%d",&result->num); // 输入员工个数 result->info = (employeeRecordT *) malloc(sizeof(employeeRecordT) * result->num ); printf ("%d:",i); //输入每个员工信息 scanf("%s%s%s%lf%d",&result->info->name, &result->info->title, &result->info->ssnum, &result->info->salary, &result->info->witholding); while (i < result->num) { result->info++; i++; printf ("%d:",i); scanf("%s%s%s%lf%d",&result->info->name, &result->info->title, &result->info->ssnum, &result->info->salary, &result->info->witholding); } result->info -= i-1; //指针前移 return result; } int main() { int i; payrollT emp; emp = Getpayroll(emp); //输出 printf("How many employees: %d\n",emp->num); for ( i = 1; i < emp->num+1 ; emp->info++, i++) { printf("Employee #%d\n",i); printf(" Name: %s\n",emp->info->name); printf(" Title: %s\n",emp->info->title); printf(" SSNum: %s\n",emp->info->ssnum); printf(" Salary: %.2lf\n",emp->info->salary); printf(" Withholding exemptions: %d\n",emp->info->witholding); } return 0; }
代码能够顺利实现输出,不过我朋友说结构体定义有问题,我不知道是什么问题,请大家给与细心解答!
[ 本帖最后由 linos 于 2014-4-27 16:41 编辑 ]