I have a question about using pointer. Cuz after i use the pointer, the program isnt work.
so i wanna change my orginial code by using pointer
Thank you'
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define merror() {printf("memory allocation problem\n");exit(1);}
int main(void)
{
char buffer[100];
char buf[100][22];
char sto[19];
int num;
int i, j;
printf(" how many names will be entered : \n");
fflush(stdout);
fgets ( buffer, 10, stdin );
num = atoi (buffer);
if (num <=0)
{
printf("empty input \n");
}
else if (num >100)
{
printf("input too long \n");
}
else if (num >0 && num <= 100)
{
printf("you will enter %d names \n", num);
}
for(i=0; i < num; i++)
{
printf(" enter a name \n");
for(j=0; j<21; j++)
{
buf[i][j]=fgetc(stdin);
if (buf[i][j]=='\n') {
buf[i][j]='\0';
break; /* break j-loop */
}
}/* end j-loop */
if (j==21)
{
printf("input too long\n");
while(fgetc(stdin)!='\n');
continue; /* continue i-loop */
}
}
printf("\nunsorted input:\n");
for(i=0; i< num; i++)
printf("%s\n", buf[i]);
for(i=num; i>0; i--)
for(j=0; j<i; j++)
if (strcmp(buf[j],buf[j+1])>0) {
strcpy(sto,buf[j]);
strcpy(buf[j],buf[j+1]);
strcpy(buf[j+1],sto);
}
printf("sorted input:\n");
for(i=0; i<=num; i++)
printf("%s\n",buf[i]);
return 0;
}