9_1.c
Write a program that uses a table of integers whose size will be specified interactively at run time.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
int *p,*table,i;
int size;
clrscr();
printf("what is the size of table");
scanf("%d",&size);
printf("\n");
table=(int *)malloc(size*sizeof(int));
printf("input table values\n");
for(p=table,i=0;i<size;i++,p++)
{
scanf("%d",p);
}
for(p=table,i=0;i<size;i++,p++)
{
printf("%d is stored at address %u\n",*p,p);
}
getch();
}
9_2.c
Write a program to store a character string in block of memory space created by malloc and then modify the same to store a large string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
void main()
{
char *buffer,*p;
int i=0;
clrscr();
buffer=(char *)malloc(10);
strcpy(buffer,"RAJKOT");
for(p=buffer,i=0;*p!='\0';p++,i++);
printf("buffer size of %d created",i);
printf("buffer contains%s",buffer);
buffer=(char *)realloc(buffer,15);
printf("buffer size modified");
printf("buffer still contain:%s\n",buffer);
strcpy(buffer,"AHMEDABAD");
for(p=buffer,i=0;*p!='\0';p++,i++);
printf("buffer size of %d created\n",i);
printf("buffer now contains:%s",buffer);
free(buffer);
getch();
}
Write a program that uses a table of integers whose size will be specified interactively at run time.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
int *p,*table,i;
int size;
clrscr();
printf("what is the size of table");
scanf("%d",&size);
printf("\n");
table=(int *)malloc(size*sizeof(int));
printf("input table values\n");
for(p=table,i=0;i<size;i++,p++)
{
scanf("%d",p);
}
for(p=table,i=0;i<size;i++,p++)
{
printf("%d is stored at address %u\n",*p,p);
}
getch();
}
9_2.c
Write a program to store a character string in block of memory space created by malloc and then modify the same to store a large string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
void main()
{
char *buffer,*p;
int i=0;
clrscr();
buffer=(char *)malloc(10);
strcpy(buffer,"RAJKOT");
for(p=buffer,i=0;*p!='\0';p++,i++);
printf("buffer size of %d created",i);
printf("buffer contains%s",buffer);
buffer=(char *)realloc(buffer,15);
printf("buffer size modified");
printf("buffer still contain:%s\n",buffer);
strcpy(buffer,"AHMEDABAD");
for(p=buffer,i=0;*p!='\0';p++,i++);
printf("buffer size of %d created\n",i);
printf("buffer now contains:%s",buffer);
free(buffer);
getch();
}
No comments:
Post a Comment