How C + + lets a function return an array

Source: Internet
Author: User

This is a very elementary problem, but it may be a headache for those who don't know. A function in C + + cannot return an array directly, but an array is actually a pointer, so it can be implemented by a function that returns a pointer. such as a function of matrix multiplication, it is easy for us to write:

1 #include <iostream> 2  3 using namespace std; 4  5 void Multmatrix (float m[4], float a[4], float b[4]) 6 {7     m[0] = a[0]*b[0] + a[1]*b[2]; 8     M[1] = a[0]*b[1] + a[1]*b[3]; 9     m[2] = a[2]*b[0] + a[3]*b[2];10     m[3] = a[2]*b[1] + a[3]*b[3];11 (cout     << m[0] << "" <<  M[1] << endl;13     cout << m[2] << "<< m[3] << endl;14}15 + int main ()     A[4] = {1.75, 0.66, 0, 1.75};19     float B[4] = {1, 1, 0, 0};20     float *m = new float[4];22     Multmatrix (M, A, B);     cout << m[0] << "<< m[1] << endl;25     cout << m[2] <<" "< < m[3] << endl;26     delete[] m;27     return 0;29}

How C + + lets a function return an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.