C++ BUG: [Error] invalid array assignment

C++BUG: [Error] invalid array assignment

1. Introduction2. The difference between the return value of memcpy() function prototype function header file and strcpy

example

1. Introduction

When using array to assign value to array, the above bug will appear. The general chestnut is as follows:

while(!student.eof()){
        SS[j].name=stud.name;//Error
        SS[j].ID=stud.ID;
        SS[j].score=stud.score;
        j++;
    }

The purpose is to store the data in the structure array object SS [J], but this assignment will report an error.

2. memcpy()

Function prototype

void *memcpy(void*dest, const void *src, size_t n);

function

The data of consecutive n bytes with SRC pointing address as the starting address is copied into the space with destination pointing address as the starting address.

Header file

The use of # include & lt; string. H & gt;;

Both # include & lt; CString & gt; and # include & lt; string. H & gt; can be used in C + +

Return value

Function returns a pointer to dest.

The difference from strcpy

1. Compared with strcpy, memcpy does not end when it encounters’ \ 0 ‘, but will copy n bytes. Therefore, we need to pay special attention to memory overflow.
2. Memcpy is used to copy memory. You can use it to copy objects of any data type, and you can specify the length of the data to be copied. Note that source and destination are not necessarily arrays, and any read-write space can be used;
however, strcpy can only copy strings, and it ends copying when it encounters’ \ 0 ‘.

example

For 2D arrays

int main(void)
{
    int src[][3]={{1,2,3},{4,5,6},{7,8,9},{1,2,3},{4,5,6},{7,8,9}};  
    int des[6][3]={0};//Be careful, the number of lines is fixed
    memcpy(des,src,sizeof(src)); //beware of memory overflow
    return 1;
}

Read More: