c - Trying to copy entire structure to another structure of same type...but getting errors -


hi friends trying copy entire structure structure. when compile, don't errors....when print displays garbage value....please guide me...thanks!

in 2 different functions trying add values , print it. first function "read_list"...in getting name of student , marks inserted user.

my second functions "print_list", using function printing details of student.

please guide me tutorials can find few quite interesting examples using structure's in c

#include<stdio.h>  typedef struct _student {     char name[50];     unsigned int mark; } student;  void print_list(student list[], int size); void read_list(student list[], int size);  int main(void) {       const int size = 3;      student list_first[size];   //first structre     student list_second[size];  //second structre of same type      read_list(list_first, size); //list_first structer fills values      print_list(list_first, size);  //to check have printed here       //now knew first struct filled data .....so wanted copy       list_second[size] = list_first[size];               //cpoid 1 struct     printf("second list copied struct: \n");     print_list(list_second, size);     //tried print here ....      system("pause");      return 0; }   void read_list(student list[], int size) {     unsigned int i;      printf("please enter info:\n");     for(i = 0; i<size; i++)     {         printf("\nname: ");         scanf("%s",&list[i].name);          printf("\nmark: ");         scanf("%d",&list[i].mark);     }      }  void print_list(student list[], int size) {         unsigned int i;      for(i=0; i<size; i++)     {         printf("name: %s\t %d\n",list[i].name,list[i].mark);     } } 

use memcpy() copy arrays:

memcpy(list_second, list_first, sizeof(list_first)); 

the current assignment attempt incorrect accessing beyond bounds of array (arrays have 0 based indexes, valid indexes run 0 size - 1) causing undefined behaviour:

list_second[size] = list_first[size]; 

even if not, copy 1 of elements , reason garbage printed because list_second uninitialized.


Comments