Program 65) Write a program that uses a table of integers whose size will be specified interactively at run time.
The program is shown below. It tests for avaibility of memory space of required size. If it is available, then the required space is allocated and the address of the first byte of the space allocated is displayed. The program also illustrates the use of pointer varaible for storing and accessing the table values.
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
main ( )
{
int *p, *table ;
int size ;
printf ("\nWhat is the size of table?") ;
scanf("%d", size) ;
printf ("\n") ;
/* - - - - - -Memory allocation - - - - - - */
if ((table = (int*) malloc (size *sizeof (int))) == NULL)
{
printf ("No space available \n") ;
exit (1) ;
}
printf ("\n Address of the first byte is %u\n", table) ;
/* Reading table values */
printf ("\nInput table values\n") ;
for (p = table; p < table + size; p++)
scanf (%d", p) ;
/* Printing table value in reverse order */
for (p = table + size -1; p >= table; p --)
printf ("%d is stored at address %u \n", *p,p) ;
}
Output: What is the size of the table? 5
Address of teh first byte is 2262
Input table values
11 12 13 14 15
15 is stored at address 2270
14 is stored at address 2268
13 is stored at address 2266
12 is storedat address 2264
11 is stored at address 2262
No comments:
Post a Comment