2014-10-06 210 views
0

我無法弄清楚如何動態地將內存分配給數組結構。我需要使用我定義的結構數組,以便可以遍歷它來打印信息。但是,我也被要求動態地分配內存給數組。結構數組中的動態分配內存(在C中)

問題是,當我在代碼中使用malloc()(您將看到它被註釋掉)時,它會將指針從數組索引中拋出。有什麼辦法來完成動態分配內存,同時指向數組指示存儲數據?

免責聲明:我對C非常陌生,如果我的編碼傷害了你的眼睛,我提前誠摯道歉。只是想了解該計劃,而不是快速完成它。

程序指令:

編寫使用 '結構' 定義包含以下學生的結構的C程序:

信息:

  1. 初始[字符]
  2. 年齡[整數]
  3. Id [整數]
  4. 等級[character]

你的程序應該爲5名學生打印上述信息。

提示:使用結構數組並遍歷數組以打印信息。

創建具有結構:int類型

  • 稱爲標籤A字符指針的

    1. 可變數據。這個指針應該指向一個字符串的地址。

    檢查內存是否可用,然後動態分配所需內存到您的結構。

    將值分配給您的結構變量並將其打印到控制檯。

    到目前爲止的代碼:

    # include <stdio.h> 
    # include <string.h> 
    # include <stdlib.h> 
    
    int main (void) 
    { 
        struct student 
        { 
         char initial [21]; 
         int age; 
         float id; 
         char grade [3]; 
        } list[5]; 
    
        struct student * tag = list; 
    
        //LINE BELOW IS WHAT I WAS TRYING TO DO; BUT THROWS POINTER OFF OF STRUCT ARRAY 
        //tag = (struct student *) malloc (sizeof (struct student)); 
        strcpy(tag->initial, "KJ"); 
        tag->age = 21; 
        tag->id = 1.0; 
        strcpy (tag->grade, "A"); 
        tag++; 
    
        strcpy(tag->initial, "MJ"); 
        tag->age = 55; 
        tag->id = 1.1; 
        strcpy (tag->grade, "B"); 
        tag++; 
    
        strcpy(tag->initial, "CJ"); 
        tag->age = 67; 
        tag->id = 1.2; 
        strcpy (tag->grade, "C"); 
        tag++; 
    
        strcpy(tag->initial, "SJ"); 
        tag->age = 24; 
        tag->id = 1.3; 
        strcpy (tag->grade, "D"); 
        tag++; 
    
        strcpy(tag->initial, "DJ"); 
        tag->age = 27; 
        tag->id = 1.4; 
        strcpy (tag->grade, "F"); 
        tag++; 
    
        int n; 
    
        for (n = 0; n < 5; n++) { 
          printf ("%s is %d, id is %f, grade is %s\n", 
           list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade); 
          } 
    
        return 0; 
    
    } 
    
  • +2

    你正在分配一個學生(提示:'sizeof(學生)'是什麼意思?),但你假裝你已經分配了一個完整的數組。這是行不通的。 – juanchopanza 2014-10-06 06:54:42

    +0

    你提到'tag'是字符指針,但是在程序中它是結構指針...給出正確的信息.. – 2014-10-06 07:15:13

    +0

    我的歉意,我對C很新。你是對的,'tag'應該是char指針。我是否在學生中聲明'tag'使其成爲字符指針?感謝'sizeof(學生)'建議 - 您的反饋真誠地感謝您。 – cjan92127 2014-10-06 07:45:08

    回答

    0

    試試這個....我在這裏聲明一個結構指針tag。我爲5個學生結構分配記憶。分配值後,現在tag指向最後一個值,所以我減少了5次。這個程序只使用一個指針。如果你想要,你可以嘗試使用數組指針。

    我在節目中提到的變化,爲//changes

    # include <stdio.h> 
    # include <string.h> 
    # include <stdlib.h> 
    
    int main (void) 
    { 
    struct student 
        { 
        char initial [21]; 
        int age; 
        float id; 
        char grade [3]; 
        } list[5]; 
    
    struct student * tag; 
    
    
        tag = (struct student *) malloc (5* sizeof (struct student));//changes 
    
    strcpy(tag->initial, "KJ"); 
    tag->age = 21; 
    tag->id = 1.0; 
    strcpy (tag->grade, "A"); 
    tag++; 
    
    strcpy(tag->initial, "MJ"); 
    tag->age = 55; 
    tag->id = 1.1; 
    strcpy (tag->grade, "B"); 
    tag++; 
    
    strcpy(tag->initial, "CJ"); 
    tag->age = 67; 
    tag->id = 1.2; 
    strcpy (tag->grade, "C"); 
    tag++; 
    
    strcpy(tag->initial, "SJ"); 
    tag->age = 24; 
    tag->id = 1.3; 
    strcpy (tag->grade, "D"); 
    tag++; 
    
    strcpy(tag->initial, "DJ"); 
    tag->age = 27; 
    tag->id = 1.4; 
    strcpy (tag->grade, "F"); 
    tag++; 
    
    int n; 
        tag=tag-5;//changes 
    
    for (n = 0; n < 5; n++) { 
         printf ("%s is %d, id is %f, grade is %s\n", 
          tag->initial, tag->age, tag->id, tag->grade); 
          tag++;//changes 
         } 
    
    return 0; 
    } 
    

    使用品特的陣列......(而不是使用單獨的任務,你可以使用循環分配值,以每一個學生)

    #include <stdio.h> 
        #include <string.h> 
        #include <stdlib.h> 
    
        int main (void) 
        { 
          struct student 
          { 
          char initial [21]; 
          int age; 
          float id; 
          char grade [3]; 
          } list[5]; 
    
         struct student *tag[5]; 
        int i; 
    
        for(i=0;i<5;i++) 
        tag[i]= (struct student *) malloc (sizeof (struct student)); 
    
         strcpy(tag[0]->initial, "KJ"); 
         tag[0]->age = 21; 
         tag[0]->id = 1.0; 
         strcpy (tag[0]->grade, "A"); 
    
         strcpy(tag[1]->initial, "MJ"); 
         tag[1]->age = 55; 
         tag[1]->id = 1.1; 
         strcpy (tag[1]->grade, "B"); 
    
         strcpy(tag[2]->initial, "CJ"); 
         tag[2]->age = 67; 
         tag[2]->id = 1.2; 
         strcpy (tag[2]->grade, "C"); 
    
         strcpy(tag[3]->initial, "SJ"); 
         tag[3]->age = 24; 
         tag[3]->id = 1.3; 
         strcpy (tag[3]->grade, "D"); 
    
         strcpy(tag[4]->initial, "DJ"); 
         tag[4]->age = 27; 
         tag[4]->id = 1.4; 
         strcpy (tag[4]->grade, "F"); 
    
         for (i = 0; i < 5; i++) 
         { 
           printf ("%s is %d, id is %f, grade is %s\n", 
          tag[i]->initial, tag[i]->age, tag[i]->id, tag[i]->grade); 
    
         } 
    
         return 0; 
    
        } 
    
    +0

    非常感謝你的malloc大小5,並減少建議!你能舉一個你指的是什麼意思的例子嗎?它只是聲明多個指針,如標籤或不同類型的指針?再次感謝你。 – cjan92127 2014-10-06 07:41:27

    0

    如果您使用指針讀取並打印結構值,則不需要數組。如果你正在嘗試使用數組,那麼這是做到這一點的方法。

    #include <stdio.h> 
        # include <string.h> 
        # include <stdlib.h> 
    
        int main (void) 
        { 
         struct student 
         { 
          char initial [21]; 
          int age; 
          float id; 
          char grade [3]; 
         } list[5]; 
    
         struct student *tag = (struct student *) malloc (sizeof (struct student) * 5); 
    
         int i; 
         for(i=0;i<5;i++) 
         { 
          printf("Enter the student initial\n"); 
          scanf("%s",list[i].initial); 
          printf("Enter the student age\n"); 
          scanf("%d",&list[i].age); 
          printf("Enter the student id\n"); 
          scanf("%f",&list[i].id); 
          printf("Enter the grade\n"); 
          scanf("%s",list[i].grade); 
          tag++; 
         } 
    
         int n; 
    
         for (n = 0; n < 5; n++) { 
          printf ("%s is %d, id is %f, grade is %s \n", 
           list [ n ].initial, list [ n ].age, list [ n ].id, list [ n ].grade); 
         } 
    
         return 0; 
    
        }