簡體   English   中英

動態結構數組

[英]Dynamic array of structures

我是初學者,使用 DMA 和 arrays 概念編碼的結構來獲取員工的詳細信息,例如姓名、地址、員工 ID 和他們在結構中的年齡,使用數組並按升序排列接收到的詳細信息。 在這里我無法弄清楚將掃描名稱存儲在結構中的問題。 當我給出超過兩個作為員工計數的值時,名稱的 output 是一些垃圾值。 請幫我解決它。 我們輸入的名稱必須顯示,但未顯示。

#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);
    }
}
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是一個指針,它的大小不是結構大小

分配給引用的object的大小,而不是指針的大小
@某位程序員哥們

// 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]);

如果可能,避免調整大小/轉換為類型並分配給引用的 object,如下所示。 更容易正確編碼、審查和維護。

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

避免使用fgets()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();
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM