C + + and C dynamic request two-dimensional arrays

Source: Internet
Author: User

This is a question I encountered in the interview, using C and C + + to apply for a two-dimensional array, int **res, the request can be used after the res[3][4] This type of anti-storage mode.

This is a version with no error checking.

Answer:

Version of the C + + language

int **allocate (int row, int column) {    int **res = new int*[row];    for (int i = 0; i < row; i++) {        res[i] = new int[column];    }    return Res;}    

C language

int **allocate (int  int  column) {    int **res = (int * *) malloc (sizeof(int *) * row);      for (int0; i < row; i++) {        = (int *) malloc (sizeof(int ) * column);    }     return Res;}

C + + and C dynamic request two-dimensional arrays

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.