C + + one-dimensional array pointers to two-dimensional arrays

Source: Internet
Author: User

1.
inta[3] = {1, 2, 3}
A represents the first address of an array
&a[0] is also the first address of the array

2.
int a[2][2] = {0, 1, 2, 3};

**a is a[0][0] first row first column.
* (*a + 1) is a[0][1] The second column in the first row.
* * (A + 1) is a[1][0] The first column in the second row.
* (* (A + 1) + 1) is a[1][1] second row second column.

3.
inta[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};//相当于是
A[0][0]=1;
a[0][1]=2;
a[0][2]=3;

4.
Array pointers
int//正确。
The above code means, define an array of PTR, the dimension is a one-dimensional array, the element is 3, and the definition is a pointer to the first address of the array, so you can assign a value, because a is the first address of the array

5.
Array of pointers
An array of pointers is an array of pointers, and the elements in the array are pointers (in contrast to an integer array, an integer array is an array of integers, and the elements in the array are integers).
int *ptr[3]; How to understand? According to the operator precedence, [] The priority is large, so ptr first with [3], indicating that PTR is an array, it is necessary to clarify the element type of the array, so the element type in the array is an integer pointer (int*), the size of the array is not necessarily required (define the array can be determined according to the number of elements initialized)
Ptr[0] is the 0th element of an array, which is an integer pointer.
Examples are as follows:
1234 inta[3] = {1, 2, 3};intx = 5;ptr[0] = &x;ptr[1] = &a[2];
2. How do I use it?
Used like a normal pointer. *PTR[0] is the value of the element that the 0th element (a pointer) points to, which is 5.

6. Array pointers
#include "stdafx.h"
int main (int argc, char* argv[])
{
int iarr[2][3]={0,1,2,3,4,5};
int *p=iarr[0];//The first address of a two-dimensional array
printf ("%d \ n", p);//1638192
printf ("%d \ n", Iarr);//1638192
printf ("%d", p[1]);
printf ("%d\n", * (p+1));//1
printf ("%d", p[2]);
printf ("%d\n", * (p+2));//2
printf ("%d", p[3]);
printf ("%d\n", * (p+3));//3

int (*parr) [3]=iarr;//This code means that a pointer to three integer elements will have more than one row and multiple columns
printf ("%d\n", * (PARR));//1638192 1638196 1638200 1638204
printf ("%d\n", * (parr+1));//1638204
printf ("%d\n", * (* (PARR)));//0
printf ("%d\n", * (* (parr+1)));//3

GetChar ();
return 0;
}
7, one-dimensional array of two-dimensional array initialization
#include "stdafx.h"
int main (int argc, char* argv[])
{
int test[3][3];
for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
printf ("%d", test[i][j]);
}
printf ("\ n");
}
-858993460858993460858993460
-858993460858993460858993460
-858993460858993460858993460
int test2[3][3]={0};
for (int i1=0;i1<3;i1++)
{
for (int j1=0;j1<3;j1++)
{
printf ("%d", test2[i1][j1]);
}
printf ("\ n");
}
-000
-000
-000
int test3[3];
for (int hh=0;hh<3;hh++)
{
printf ("%d", test3[hh]);
}
printf ("\ n");
printf ("\ n");
-858993460858993460858993460
int test4[3]={0};
for (int hh1=0;hh1<3;hh1++)
{
printf ("%d", TEST4[HH1]);
}
printf ("\ n");
printf ("\ n");
0 0 0
GetChar ();
return 0;
}

8. SizeOf array pointer
sizeof array does not degenerate into a pointer
#include "stdafx.h"
int main (int argc, char* argv[])
{
At this point, the passed array name is no longer the function of the pointer
int test[]={0,1,2,3,4,5};
int *p=test;
printf ("Test takes up Bytes:%d\n", sizeof (test));//24 occupies 24 bytes
printf ("Test takes up Bytes:%d\n", sizeof (&test));//24 takes 24 bytes
printf ("p occupies a number of bytes:%d\n", sizeof (p));//4 pointer occupies 4 bytes

int test2[20]={0,1,2,3,4,5};
printf ("test2-occupied bytes:%d\n", sizeof (TEST2)),//80 takes up 80 bytes

GetChar ();
return 0;
}

9. Strlen Array pointers
Strlen arrays are degraded to pointers
#include "stdafx.h"
#include <string.h>
int main (int argc, char* argv[])
{
Char *p= "123456";
printf ("%d\n", strlen (p));//6
Char test[20]={1,2,3,4,5,6};
printf ("%d\n", strlen (test));//6
Char test1[]={1,2,3,4,5,6};
printf ("%d\n", strlen (Test1));//14
Char test2[2]={0};
printf ("%d\n", strlen (Test2));//0
Char test3[2];
printf ("%d\n", strlen (TEST3));//4
Char test4[20];
printf ("%d\n", strlen (test4));//24
Char test5[20]={1};
printf ("%d\n", strlen (TEST5));//1
Char test6[20]={0};
printf ("%d\n", strlen (TEST6));//0
GetChar ();
return 0;
}
10. The pointer array is passed as a parameter
#include "stdafx.h"
#include <string.h>
Working with arrays
void Processarray (char arrary[])
{
printf ("%s\n", arrary);//My
}
Working with integer arrays
void Processarray (int arrary[])
{
printf ("%d\n", arrary[0]);//My
}
Handling pointers
void Processpointer (int *p)
{
printf ("%d\n", * (p+1));
}
Handling references
void processref (int &p)
{
printf ("%d\n", p);
}
int main (int argc, char* argv[])
{
Char a[10]={"my"};
int test[10]={0,1,2,3,4,5};
int *p=test;
int k=5;
Processarray (a);//My
Processarray (test);//0
Processpointer (p);//1
Processref (k);//5
return 0;
}


C + + one-dimensional array pointers to two-dimensional arrays

Related Article

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.