Here is the sample of my code. I want to create dynamic character array for storing string.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i , j , n;
char *ptr;
printf("enter number of elements \n");
scanf("%d",&n);
ptr = (char *) malloc((n + 1)*sizeof(char));
for (i = 0;i< n;i++)
{
scanf("%c",&ptr[i]);
}
for ( i = 0;i <n; i++)
{
printf("at %d is %c\n",i,*(ptr + i));
}
free(ptr);
}
But when I try to compile and run this code, no character is assigned to memory which is pointed by pointer p
.
Here is the output of my program:
jharvard@appliance (~/c): ./test2
enter number of elements
8
asdfghjk
at 0 is
at 1 is a
at 2 is s
at 3 is d
at 4 is f
at 5 is g
at 6 is h
at 7 is j