1

我有以下代码:

#define NUM_STUDENTS 20
#define TIME_STUDENTS 10

typedef struct
{
    int name;
    int age;
} Student;

typedef struct
{
    int number;
    int post;
} Contact;

typedef struct
{
      int             number;
      Student         students[TIME_STUDENTS][NUM_STUDENTS];
      Contact         contact[NUM_STUDENTS];
} Master;

typedef struct
{
      int             number;
      Student         students[NUM_STUDENTS][NUM_STUDENTS];
      Contact         contact[NUM_STUDENTS];
} Info;

Info info;
Master master;

//fill the data for master

if(NUM_STUDENTS < 10)
{
   memcpy(&info.student[0][0],
         &master.student[0][0],
         sizeof(Student) * NUM_STUDENTS * NUM_STUDENTS);
}

NUM_STUDENTS 可以从修改120。但我收到以下警告:

Warning 420: Apparent access beyond array for function 'memcpy(void *, const void *, unsigned int)', argument 3 exceeds argument 2

有什么问题以及如何解决?

4

1 回答 1

6

Master中,你只有

Student         students[TIME_STUDENTS][NUM_STUDENTS];

并且TIME_STUDENTS小于NUM_STUDENTS,所以

 memcpy(&info.student[0][0],
       &master.student[0][0],
       sizeof(Student) * NUM_STUDENTS * NUM_STUDENTS);

复制比源更多的字节。

于 2013-06-14T16:03:55.773 回答