You are on page 1of 3

DMA

/*A program to read & display elements using DDA*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],m,n,i,j;
clrscr();
printf("Enter no.of rows and cols: ");
scanf("%d %d",&m,&n);
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The array elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
getch();
}
Output:
Enter no.of rows and cols: 2 2
Enter the matrix elements 1 2 3 4
The array elements are
12
34

/*A program to read & display elements using DDA with DMA*/
#include<stdio.h>
#include<conio.h>
void main()
{
int **a,m,n,i,j;
clrscr();
printf("Enter no.of rows and cols: ");
scanf("%d %d",&m,&n);
a=(int **)malloc(m*sizeof(int));
for(i=0;i<m;i++)
a[i]=(int *)malloc(n*sizeof(int));
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
//scanf("%d",*(a+i)+j);
printf("The array elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
//printf("%d ",*(*(a+i)+j));
printf("\n");
}
getch();
}
Output:
Enter no.of rows and cols: 2 2
Enter the matrix elements 1 2 3 4
The array elements are

/*A program to read & display elements using DDA with DMA*/
/*Array of Pointer*/
#include<stdio.h>
#include<conio.h>
void main()
{
int *a[10],m,n,i,j;
clrscr();
printf("Enter no.of rows and cols: ");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
a[i]=(int *)malloc(n*sizeof(int));
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
//scanf("%d",*(a+i)+j);
printf("The array elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
//printf("%d ",*(*(a+i)+j));
printf("\n");
}
getch();
}
Output:
Enter no.of rows and cols: 2 2
Enter the matrix elements 1 2 3 4
The array elements are
12
34
/*A program to read & display elements using DDA with DMA*/
#include<stdio.h>
#include<conio.h>
void main()
{
int *a,m,n,i,j;
clrscr();
printf("Enter no.of rows and cols: ");
scanf("%d %d",&m,&n);
a=(int *)malloc(m*n*sizeof(int));
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",a+i*n+j);
printf("The array elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",*(a+i*n+j));
printf("\n");
}
getch();
}
Output:
Enter no.of rows and cols: 2 2
Enter the matrix elements 1 2 3 4
The array elements are
12
34

12
34
/*A program to read & display elements using DDA with DMA*/
/*Pointer to Array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int (*a)[10],m,n,i,j;
clrscr();
printf("Enter no.of rows and cols: ");
scanf("%d %d",&m,&n);
a=(int *)malloc(m*sizeof(int));
printf("Enter the matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
//scanf("%d",*(a+i)+j);
printf("The array elements are \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
//printf("%d ",*(*(a+i)+j));
printf("\n");
}
getch();
}

/*W.A.P to interchange two numbers in calling function from


called function*/
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b,*aptr,*bptr;
clrscr();
printf("\nEnter any two numbers: ");
scanf("%d%d",&a,&b);
aptr=&a; bptr=&b;
printf("\nBefore swap a = %d\tb = %d",a,b);
swap(aptr,bptr);
printf("\nAfter swap a = %d\tb = %d",a,b);
getch();
}
void swap(int *x,int *y)
{
*x=*x+*y;
*y=*x-*y;
*x=*x-*y;
}
Output:
Enter any two numbers: 20 30

Output:
Enter no.of rows and cols: 2 2
Enter the matrix elements 1 2 3 4
The array elements are
12
34

Before swap a = 20
After swap a = 30

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2]={{10,20},{30,40},{50.60}};
int i;
clrscr();
for(i=0;i<3;i++)
printf("a[%d] array address = %u\n",i,a[i]);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5]={{10,20},{30,40}};
int i,j;
clrscr();
printf("The matrix elements are\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d ",*(*(a+i)+j));
printf("\n");
}
getch();
}

Output:
a[0] array address = 65514
a[1] array address = 65518
a[2] array address = 65522

b = 30
b = 20

Output:
The matrix elements are
10 20
30 40
/*A Program on void pointer*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
int *p=&a,*q=&b;
void *v1=p,*v2=q;

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
char *names[10];
int i,n;
clrscr();

clrscr();
printf("%d ",*((int *)v1));
printf("%d",*((int *)v2));
getch();

printf("Enter no. of names: ");


scanf("%d",&n);
for(i=0;i<n;i++)
names[i]=(char *)malloc(n*sizeof(char));
printf("Enter names\n");
for(i=0;i<n;i++)
{
printf("Enter %d name: ",i+1);
scanf("%s",names[i]);
}
printf("The names are\n");
for(i=0;i<n;i++)
puts(names[i]);
getch();

}
Output:
10 20

}
Output:
Enter no. of names: 3
Enter names
Enter 1 name: aaa
Enter 2 name: sss
Enter 3 name: dff
The names are
aaa
sss
dff
/*An example program on Free*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
float *a;
clrscr();
a=(float *)malloc(5*sizeof(float));
if(a==NULL)
{
printf("Memory is not allocated");
}
else
{
printf("Memory is allocated");
}
free(a);
getch();
}

/*An example program on Free*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
float *a;
clrscr();
a=(float *)malloc(0*sizeof(float));
if(a==NULL)
{
printf("Memory is not allocated");
}
else
{
printf("Memory is allocated");
}
free(a);
getch();
}

Output:
Memory is allocated

Output:
Memory is not allocated

/*Aprogram od NULL pointer*/


#include<stdio.h>
#include<conio.h>
void main()
{
int *p=NULL;
clrscr();
printf("%d",p);
getch();
}
Output:
0

You might also like