You are on page 1of 2

Dynamic Memory Allocation /* Program for dynamic memory allocation */ #include<stdio.h> #include<conio.h> #include<stdlib.

h> #include <iostream> using namespace std; class ABC{ int n, *a, i,j,k; int p,q, **b; public : ABC() {cout<<"enter size of 1d array";//printf("\n enter the value of n:"); cin>>n; } //scanf("%d", &n);

// memory allocation to 1-D poin ter variable void OneDArray() { a=(int *)calloc(sizeof(int), n); for(i=0;i<n;i++) { cout<<"\n value of a["<<i<<"] is "<< a[i]; //printf("\n value of a[%d] is %d", i, a[i]); } } void twoArray() { cout<<"\n enter the values of p and q"; //printf("\n enter the values of p and q"); cin>>p>>q;//scanf("%d%d",&p,&q); // memory allocaation to 2-D pointer varaible b=(int **)calloc(sizeof(int *), p);

for(i=0;i<p;i++) { b[i]=(int *)calloc(sizeof(int),q); } k=100; for(i=0;i<p;i++) { for(j=0;j<q;j++) { b[i][j]=k; k++; } } for(i=0;i<p;i++) { cout<<"\n"; // printf("\n"); for(j=0;j<q;j++) { cout<<"\t"<<*(*(b+i)+j); //printf("\t %d",*(*(b+i)+j)); } } } } }; void main() { ABC obj; obj.OneDArray(); obj.twoArray(); }

You might also like