I am trying to store character as well as integer numbers in the same region through character pointer however, I am not getting the intended result. It is displaying only characters. Here, I have created a character pointer which should contain atleast both characters and integers, then should display them properly.
int main()
{
char c,store[30];
char *p=malloc(30);
int choice,i=0,n;
p=store;
while(1)
{
printf("\n********Menu**********");
printf("\n1.Enter a character");
printf("\n2.Enter a Number");
printf("\n3.Enter a double");
printf("\n4.Display the values");
printf("\n0.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a character:");
fflush(stdin);
scanf("%c",&c);
*p=c;
p++;
break;
case 2:
printf("\nEnter a Number");
scanf("%d",&n);
*p++=n;
break;
case 4:
*p--='\0';
for(i=0;store[i];i++)
printf("%c",store[i]);
break;
case 0:
exit(0);
break;
}
}
getch();
return 0;
}
You can't simply write doubles and int into a char
array.
However with following you could see 0-9 :
case 2:
printf("\nEnter a Number"); //Just for 0-9
scanf("%d",&n);
*p=n+48; //Convert to ascii
p++;
break;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.