/* mallstr.c:
 *  Richard Kay, last changed 10 Oct 2008
 *  Demonstrates dynamic string memory allocation 
 *  */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h> /* need to include stdlib for malloc()*/

int main(void){
	char *sp; /* space declared for string address, none allocated to string*/
	int i,size; /* size of memory for string */
	printf("enter size of string\n");
	scanf("%d",&size);
	fflush(stdin);
  
	/* allocate memory for string and assign start address to sp*/
  
	sp=(char*)malloc((size_t)size*sizeof(char)); 
	/* buffer of size bytes starting address assigned to sp */
	printf("enter string\n"); /* get string and input it to buffer allocated */
	fgets(sp,size,stdin);  /* safer than scanf("%s",sp) as buffer can't overflow */

	/* output string one char at a time */

	printf("pos \tchar \tascii\n");  /* column headings */
	for(i=0;i<size;i++)    /* output position,char and decimal ascii values */
		printf("%d\t%c\t%d\n",i,sp[i],sp[i]);  /* note use of array notation    */
                                            /* sp[i] is the same as *(sp+i)   */
	getch();
	return 0;
}