About returning two-dimensional array pointer problems

Source: Internet
Author: User

The so-called two-dimensional array pointer, pointer, refers to the two-dimensional array in memory of the storage address.

The difference between the address of a two-dimensional array and the address of a one-dimensional array is that it has the address of a line in addition to the element address, which identifies the beginning of each row (called the first address of the row).
The first address of the row and the address of the first element of the line have the same address value, but they are two different addresses: if there is an int a[5][5], then a[0][0] is an array of a

The first line column element (representing the value of the element). &a[0][0] is the address of the first line element. &&a[0][0] is the first address of the first line. In this sense, you can
The first address of a line is a double address, which is a pointer to a pointer.

Don't say much nonsense, put the code first:

1>Created time:2016 September 29 Thursday 18:23 12 seconds2************************************************************************/3 4#include <stdio.h>5#include <stdlib.h>6 #defineN 57 8 int**multi (intX[n][n],intY[n][n])9 {Ten     int**T; OneT= (int**)malloc(sizeof(int*)*n); A     inti; -      for(i=0; i<n;i++) -     { theT[i]= (int*)malloc(sizeof(int)*n); -     } -  -     intA=0, b=0; +      for(a=0; a<n;a++) -     { +          for(b=0; b<n;b++) A         { at             intk=0; -              for(k=0; k<n;k++) -             { -t[a][b]+=x[a][k]*Y[k][b]; -             } -         } in     } -  to     returnT; + } - intMain () the { *     intA[n][n]; $     intB[n][n];Panax Notoginseng  -     intI=0, j=0; the      for(i=0; i<n;i++) +     { A          for(j=0; j<n;j++) the         { +a[i][j]=1; -b[i][j]=2; $         } $     } -  -     int**ans; theans=multi (A, b); - Wuyi     intC=0, d=0; the      for(c=0; c<n;c++) -     { Wu          for(d=0;d <n;d++) -         { Aboutprintf"%d", Ans[c][d]); $         } -  -printf"\ n"); -     } A  +     return 0; the}
View Code

This is the code that multiplies the two simple matrices written in C. mainly see int **multi (int x[n][n],int y[n][n]), this function, which returns a two-dimensional array pointer.

Return T at the end of the function; T is a two-dimensional array pointer, which is a pointer to pointers.

Error code

1#include <stdio.h>2#include <stdlib.h>3 #defineN 54 5 int**multi (int**x,int**y)6 {7     int**T;8T= (int**)malloc(sizeof(int*)*n);9     inti;Ten      for(i=0; i<n;i++) One     { AT[i]= (int*)malloc(sizeof(int)*n); -     } -  the     intA=0, b=0; -      for(a=0; a<n;a++) -     { -          for(b=0; b<n;b++) +         { -             intk=0; +              for(k=0; k<n;k++) A             { att[a][b]+=x[a][k]*Y[k][b]; -             } -         } -     } -  -     returnT; in } - intMain () to { +     intA[n][n]; -     intB[n][n]; the  *     intI=0, j=0; $      for(i=0; i<n;i++)Panax Notoginseng     { -          for(j=0; j<n;j++) the         { +a[i][j]=1; Ab[i][j]=2; the         } +     } -  $     int**ans; $ans=multi (A, b); -  -     intC=0, d=0; the      for(c=0; c<n;c++) -     {Wuyi          for(d=0;d <n;d++) the         { -printf"%d", Ans[c][d]); Wu         } -  Aboutprintf"\ n"); $     } -  -     return 0; -}
View Code

I changed the parameter in the function to int **x,int **y;

Error, which indicates that int a[n][n],a is int (*) [5], that is, the address represented by a is the first address of a column of a two-dimensional array, which is a one-dimensional array pointer (the two-dimensional array is essentially an array of array elements, i.e. an array of arrays), and int **x,x is a two-dimensional array pointer.



About returning two-dimensional array pointer problems

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.