Example details of C and C++ dynamic array creation

  • 2020-06-01 10:26:21
  • OfStack

Example details of C/C++ dynamic array creation

In C++ language, 2d dynamic array is mainly established by pointer method. Take the establishment of an integer 2d array as an example:


#include<iostream>
#include<string>
#include<malloc.h>
using namespace std;
int main(int argc,char **argv)
{
 ///*int a[2][3]={{1,2,3},{4,5,6}};
 //cout<<sizeof(a+1)<<endl;*/
 //int a=4;
 //int **pp;
 //pp=(int **)malloc(sizeof(int*)*a);
 //int aa[5][1]={1,2,3,4,5};
 //return 0;
int column,row; cout<<" The input 2 The number of rows and columns in a dimension array "<<endl; 
cin>>row>>column;
int **array;
array = (int **)malloc(sizeof(int *)*row);
for(int i=0;i!=row ; i++)
 array[i]=(int *) malloc(sizeof(int )*column);
cout<<" The input 2 Dimensional array "<<endl;
for(int j=0 ; j !=row ; j++)
{for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } } 
cout<<" The input of 2 Dimensional array is "<<endl; 
for( int j=0 ; j !=row ; j++ ) 
{ for(int k=0 ; k !=column ; k++) 
{cout<<array[j][k]<<" "; } 
cout<<endl; } 
// Release the space    
for(int i=0 ;i!=row;i++)
 free(array[i]); 
free(array);
return 0;
}

Dynamically create a 1-dimensional array


int len;
 cout<<" The input 1 Dimension array size :"<<endl;
 cin>>len;
 int *p=new int[len];
 
 cout<<" Enter elements separated by Spaces !"<<endl;
 int val,i=0;
 for(i=0;i!=len;i++)
 {cin>>val;
 p[i]=val;
 }
 cout<<" The output 1 Dimensional array :"<<endl;
 for(i=0;i!=len;i++)
 {
 
 cout<<p[i]<<" ";
 }
 cout<<endl;

Dynamically allocate a 2-dimensional array


int main(int argc,char **argv)
{

int column,row; 
cout<<" The input 2 The number of rows and columns in a dimension array "<<endl; 
cin>>row>>column;
int **array;
//array = (int **)malloc(sizeof(int *)*row);// methods 1
 array=new int *[row];
for(int i=0;i!=row ; i++)
 //array[i]=(int *) malloc(sizeof(int )*column);// methods 1
 array[i]=new int [column];
cout<<" The input 2 Dimensional array "<<endl;
for(int j=0 ; j !=row ; j++)
{for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } } 
cout<<" The input of 2 Dimensional array is "<<endl; 
for( int j=0 ; j !=row ; j++ ) 
{ for(int k=0 ; k !=column ; k++) 
{cout<<array[j][k]<<" "; } 
cout<<endl; } 
// Release the space    
for(int i=0 ;i!=row;i++)
 free(array[i]); 
free(array);
return 0;
}

C++ dynamically creates an array inside the struct, and creates an array of dynamic structs

Just look at the example of 1!


int main(int argc, char* argv[])
{
int n,i,m,j;
  struct test
{
  int *array;
};
test *testarray;
cin>>n>>m;
testarray=new test[m];
for (i=0;i<m;i++)
{
testarray[i].array=new int[n];
}
for (i=0;i<m;i++)
{
  for (j=0;j<n;j++)
  {
  testarray[i].array[j]=i+j;
  }
 
}
for (i=0;i<m;i++)
{
  for (j=0;j<n;j++)
  {
  cout<<testarray[i].array[j];
  
  }
  cout<<endl;
}
return 0;
}

The general idea is that you make an array of structs and declare a dynamic array inside each element!

Just instantiate and then declare a dynamic array inside the instantiated element!

You can test it by filling it with something 1!

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: