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:
- Tensorflow C++:You must define TF_LIB_GTL_ALIGNED_CHAR_ARRAY for your compiler
- C++ Compile Error: error: invalid conversion from ‘void*‘ to ‘char*‘ [-fpermissive]
- Linux C++ Error: invalid use of incomplete type [How to Solve]
- [Solved] TensorFlow Error: ‘Tensor‘ object does not support item assignment
- Vue+TS main.ts error: unused expression, expected an assignment or function call
- g++: internal compiler error: Killed (program cc1plus) Please submit a full bug report, with preprocess
- C++:error C2872: ‘byte‘: ambiguous symbol [How to Solve]
- [Solved] Halcon & C# Error: HalconDotNet.HOperatorException:“HALCON error #5190: Invalid window parameter in op
- Error: array bound is not an integer constant before ‘]’ token
- [HBase Error]“java.lang.OutOfMemoryError: Requested array size exceeds VM limit”
- [Solved] Tensorflow/Keras Error reading weights: ValueError: axes don‘t match array
- [Solved] Appium Error: InvalidArgumentException: Message: invalid argument: invalid locator
- [Solved] SyntaxError: Invalid regular expression: invalid group specifier name
- RuntimeError: implement_array_function method already has a docstring(Pycharm install package error)
- How to Solve Fatal error stdatomic in C/C++ Compilation
- Grpc Compilation issues: “C++ versions less than C++11 are not supported.
- Testlink 1.9.16 Error: Fatal error: Uncaught Error: Cannot use string offset as an array in D:\softe
- Cmake Setting Support C++11 This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options
- OpenCV(-206:Bad flag (parameter or structure field)) Unrecognized or unsupported array type [How to Solve]
- [Solved] URIError: Failed to decode param ‘/%3C%=%20BASE_URL%20%3Estatic/index.%3C%=%20VUE_APP_INDEX_CSS_HASH%20%3E.css’