#8375: 有關C語言鏈結串列結構問題


chocolate (chocolate)


我在printf結構資料都能正常出來,但字串部分就無法正常顯示字串,請協助解惑,謝謝

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

struct data
{
     short int num;
     char name[20];
     int math;
     int lv;
     struct data *next;
};

typedef struct data NODE;


int main(void)
{
NODE  array[12]={{1,"Chocolate",99,1,NULL},
                 {2,"Sky",95,2,NULL},
                 {3,"Dwight Howard",56,9,NULL},
                 {4,"Jeremy Lin",92,3,NULL},
                 {5,"Chandler Parsons",90,4,NULL},
                 {6,"Omer Asik",78,8,NULL},
                 {7,"Patrick Beverley",46,10,NULL},
                 {8,"James Harden",82,7,NULL},
                 {9,"Blake Griffin",88,5,NULL},
                 {10,"Ricky Rubio",84,6,NULL}};


NODE *first,*current,*previous;
printf("%d\n",sizeof(NODE));
int i;

  for(i=0;i<12;i++)
  {
  current=(NODE*)malloc(20*sizeof(NODE));
  current->name[20]=array[i].name[20];
  current->num=array[i].num;
  current->math=array[i].math;
  current->lv=array[i].lv;
  printf("%5d  %5d  %25s  %5d\n",current->num,current->math,current->name,current->lv);
     if(i==0)
        first=current;
       
    else
        previous->next=current;
        current->next=NULL;
        previous=current;
 
}

    
    system("pause");
    return 0 ;
    }

#8376: Re:有關C語言鏈結串列結構問題


chocolate (chocolate)


我在printf結構資料都能正常出來,但字串部分就無法正常顯示字串,請協助解惑,謝謝

 #include
#include

struct data
{
     short int num;
     char name[20];
     int math;
     int lv;
     struct data *next;
};

typedef struct data NODE;


int main(void)
{
NODE  array[12]={{1,"Chocolate",99,1,NULL},
                 {2,"Sky",95,2,NULL},
                 {3,"Dwight Howard",56,9,NULL},
                 {4,"Jeremy Lin",92,3,NULL},
                 {5,"Chandler Parsons",90,4,NULL},
                 {6,"Omer Asik",78,8,NULL},
                 {7,"Patrick Beverley",46,10,NULL},
                 {8,"James Harden",82,7,NULL},
                 {9,"Blake Griffin",88,5,NULL},
                 {10,"Ricky Rubio",84,6,NULL}};


NODE *first,*current,*previous;
printf("%d\n",sizeof(NODE));
int i;

  for(i=0;i<12;i++)
  {
  current=(NODE*)malloc(sizeof(NODE));
  current->name[20]=array[i].name[20];
  current->num=array[i].num;
  current->math=array[i].math;
  current->lv=array[i].lv;
  printf("%5d  %5d  %25s  %5d\n",current->num,current->math,current->name,current->lv);
     if(i==0)
        first=current;
       
    else
        previous->next=current;
        current->next=NULL;
        previous=current;
 
}

    
    system("pause");
    return 0 ;
    }


更正一下