C语言 结构的动态阵列

kx5bkwkv  于 2022-12-29  发布在  其他
关注(0)|答案(2)|浏览(173)

我是一个初学者,使用DMA和数组结构概念编码来获取雇员的详细信息,如姓名、地址、雇员ID和年龄,使用数组并按升序排列接收到的详细信息。在这里,我无法解决将扫描的姓名存储在结构中的问题。当我给予两个以上的雇员计数值时,那么输出的名称是一些垃圾值。请帮助我修复它。我们正在输入的名称必须显示,但它没有显示。

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

struct emp_details
{
    char *address;
    char *name;
    char *Emp_ID;
    int age;
};

void ascen_str(struct emp_details* employees_data,int num_of_emp);

int main()
{
    int num_of_emp;
    printf("Enter the number of employees: ");
    scanf("%d",&num_of_emp);
    struct emp_details* employees_data = malloc(num_of_emp*sizeof (employees_data));
    int i=0;
    for(int i=0;i<num_of_emp;i++)
    {
        employees_data[i].name=(char*)malloc(30*sizeof(char));
        printf("Employe %d enter the Name: ",i+1);
        scanf("%s",employees_data[i].name);
        printf("%s",employees_data[i].name);
        getchar();
        //sscanf(emp_name,"%s\n",employees_data[i].name);
        //fgets(employees_data[i].name,30,stdin);
        employees_data[i].address = (char*)malloc(100*sizeof(char));
        printf("Employe %d enter the Address: ",i+1);
        //scanf("%s",employees_data[i].address);
        fgets(employees_data[i].address,30,stdin);
        employees_data[i].Emp_ID=(char*)malloc(10*sizeof(char));
        printf("Employe %d enter the Employe ID: ",i+1);
        scanf("%s",employees_data[i].Emp_ID);
        printf("Employe %d enter the Age: ",i+1);
        scanf("%d",&employees_data[i].age);
      
    }
    printf("\n\tEmploye details before sorting\n");
    for(int i=0;i<num_of_emp;i++)
    {    
        printf("Employe %d Name: %s\n",i+1,employees_data[i].name);  
        printf("Employe %d Address: %s",i+1,employees_data[i].address);  
        printf("Employe %d Age: %d\n",i+1,employees_data[i].age);
        printf("Employe %d ID: %s\n\n",i+1,employees_data[i].Emp_ID);
    }   
    ascen_str(employees_data,num_of_emp);
    free(employees_data);
}

void ascen_str(struct emp_details* employees_data, int num_of_emp)
{
    struct emp_details temp;
    for (int i = 0; i < num_of_emp; i++) {
        for (int j = i + 1; j < num_of_emp; j++) {
            if (strcmp(employees_data[i].name, employees_data[j].name) > 0) {
                temp = employees_data[i];
                employees_data[i] = employees_data[j];
                employees_data[j] = temp;
            }
        }
    }
    printf("\n\tEmploye details in ascending order by name\n");
    for (int i = 0; i < num_of_emp; i++) 
    {
        printf("Employe Name: %s\n",employees_data[i].name);  
        printf("Employe Address: %s",employees_data[i].address);  
        printf("Employe Age: %d\n",employees_data[i].age);
        printf("Employe ID: %s\n\n",employees_data[i].Emp_ID);
    }
}
fdbelqdn

fdbelqdn1#

分配到被引用对象的大小,而不是指针的大小

@某个程序员老兄

// struct emp_details* employees_data = malloc(num_of_emp*sizeof (employees_data));
struct emp_details* employees_data = malloc(num_of_emp * sizeof employees_data[0]);

如果可能,避免调整/强制转换为 * 类型 *,并分配给被引用的对象,如下所示。这样更容易正确编码、检查和维护。

ptr = malloc(sizeof prt[0] * number_of_elements);

避免使用scanf()@Fe2O3

只使用fgets()如何?创建2个辅助函数。

// Minimal error checking
int get_int() {
  char buf[1000];
  if (fget(buf, sizeof buf, stdin) == NULL) {
    return 0;
  }
  int val;
  if (sscanf(buf, "%d", &val) == 1) {
    return val;
  }
  return 0;
}  

// Allocate the string size based on length of input.
// Minimal error checking
char *get_string() {
  char buf[1000];
  if (fget(buf, sizeof buf, stdin) == NULL) {
    return NULL;
  }
  size_t len = strlen(buf);
  if (len > 0 && buf[len-1] == 0) {
    buf[--len] = 0; // lop off potential \n
  }
  char *s = malloc(len + 1);
  if (s) {
    strcpy(s, buf);
  }
  return s;
}

我们可以在以后改进这些辅助函数,以检测冗长的输入、int溢出、文件结束......

简化记录阅读

// Error checking omitted for brevity.
for(int i=0; i < num_of_emp; i++) {
    printf("Employe %d enter the Name: ",i+1);
    employees_data[i].name = get_string();

    printf("Employe %d enter the Address: ",i+1);
    employees_data[i].address = get_string();

    printf("Employe %d enter the Employe ID: ",i+1);
    employees_data[i].Emp_ID = get_string();

    printf("Employe %d enter the Age: ",i+1);
    employees_data[i].age = get_int();
  }
}
mcdcgff0

mcdcgff02#

struct emp_details* employees_data = malloc(num_of_emp*sizeof (employees_data));

将此语句替换为

struct emp_details* employees_data = malloc(num_of_emp*sizeof (struct emp_details ));

因为employees_data是一个指针,它的大小不是结构的大小

相关问题