The C language function returns a two-bit array function parameter as a two-dimensional array

Source: Internet
Author: User

The typedef can be implemented simply. You can also write directly.

A simple example of a function that wrote two simple matrix operations.

#include <stdio.h> #include <stdlib.h>const int ROW = 3;const int COL = 4;typedef int (* mat_pointer) [col];mat_  Pointer Init_mat (Mat_pointer a) {for    (int i = 0; i < ROW, ++i) for        (int j = 0; j < COL; ++j)            a[i][j] = i + J;    return A;} void Print_mat (Mat_pointer a) {for    (int i = 0; i < ROW, ++i) for        (int j = 0; j < COL; ++j)            printf ("a[%d] [%d]=%d%c, I, J, a[i][j], j = = COL-1? ' \ n ': ' \ t ');    printf ("\ n");} int (*build_mat (void)) [col]{    Int (* arr_pointer) [col] = (int (*) [col]) malloc (ROW * COL * sizeof (int));    for (int i = 0, i < ROW; ++i) {for        (int j = 0; j < COL; ++j)            Arr_pointer[i][j] = i * + j;    }    return arr_pointer;} int main (void) {    int arr[row][col];    Mat_pointer arr2 = Init_mat (arr);    Print_mat (ARR2);    int (*ARR3) [COL] = Build_mat ();    Print_mat (ARR3);    return 0;}

  

The

C language function returns a two-bit array function parameter as a two-dimensional 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.