Last active
February 22, 2021 10:12
-
-
Save cypher-nullbyte/e47276b97f75d8867a983410ba1cbbdc to your computer and use it in GitHub Desktop.
2D Arrays_using pointers(all 4 ways) in c/c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*2D array using array of pointers to 1D_array(or block of linear memory) | |
--------------------------- | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
int main() | |
{ | |
int r,c; | |
scanf("%d%d",&r,&c); | |
int *arr[r]; | |
for(int i=0;i<r;i++) | |
arr[i]=(int *)malloc(c*sizeof(int)); | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
scanf("%d",(*(&arr[0]+i)+j)); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
printf("%d\t",*(*(arr+i)+j)); | |
printf("\n"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1. Single pointer | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int r,c; | |
cin>>r>>c; | |
int *arr=new int[r*c]; | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
cin>>*(arr+i*c+j); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
cout<<*(arr+i*c+j)<<'\t'; | |
cout<<endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*2D array using int type pointer | |
-------------------------- | |
*/ | |
#include<stdio.h> | |
#include<stdlib.h> | |
int main() | |
{ | |
int r,c; | |
scanf("%d%d",&r,&c); | |
int *arr[r]; | |
for(int i=0;i<r;i++) | |
arr[i]=(int *)malloc(c*sizeof(int)); | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
scanf("%d",(*(&arr[0]+i)+j)); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
printf("%d\t",*(*(arr+i)+j)); | |
printf("\n"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//2. Using an array of pointers | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int r,c; | |
cin>>r>>c; | |
int *arr[r]; | |
for(int i=0;i<r;i++) | |
arr[i]=new int[c]; | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
cin>>*(*(arr+i)+j); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
cout<<*(*(arr+i)+j)<<'\t'; | |
cout<<endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//2D_array using pointer to pointer | |
#include<stdio.h> | |
#include<stdlib.h> | |
int main() | |
{ | |
int r,c; | |
scanf("%d%d",&r,&c); | |
int **arr=(int *)malloc(r*sizeof(int *)); | |
for(int i=0;i<r;i++) | |
*(arr+i)=(int *)malloc(c*sizeof(int)); | |
//or arr[i]=(int *)malloc(c*sizeof(int)); | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
scanf("%d",(*(arr+i)+j)); | |
//or &arr[i][j] | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
//printf("%d\t",((arr+i)+j)); | |
printf("%d\t",arr[i][j]); | |
printf("\n"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//3. Using pointer to pointer | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int r,c; | |
cin>>r>>c; | |
int **arr=new int* [r]; | |
for(int i=0;i<r;i++) | |
arr[i]=new int[c]; | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
cin>>*(*(arr+i)+j); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
cout<<*(*(arr+i)+j)<<'\t'; | |
cout<<endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using double pointer and one malloc call | |
#include<stdio.h> | |
#include<stdlib.h> | |
int main() | |
{ | |
int r,c; | |
scanf("%d%d",&r,&c); | |
int **arr,*ptr; | |
int len=sizeof(int *)*r+sizeof(int)*c*r; | |
arr=(int **)malloc(len); | |
ptr=(int *)(*arr+r); | |
for(int i=0;i<r;i++) | |
arr[i]=(ptr+c*i); | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
scanf("%d",(*(arr+i)+j)); | |
//or &arr[i][j] | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
//printf("%d\t",((arr+i)+j)); | |
printf("%d\t",arr[i][j]); | |
printf("\n"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//4. Using double pointer and one new call. | |
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int r,c; | |
cin>>r>>c; | |
int **arr=new int* [sizeof(int*)*r +sizeof(int)*r*c]; | |
int *ptr=(*arr+r); | |
for(int i=0;i<r;i++) | |
arr[i]=ptr+c*i; | |
for(int i=0;i<r;i++) | |
for(int j=0;j<c;j++) | |
cin>>*(*(arr+i)+j); | |
for(int i=0;i<r;i++) | |
{ | |
for(int j=0;j<c;j++) | |
cout<<*(*(arr+i)+j)<<'\t'; | |
cout<<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 11 in 2d4.cpp & line 12 2d4.c :- don;t deference .