Output not satisfactory in C program due to scanf function -
i executing program. on running program, first asks " enter number of test cases" , after enter number of test cases . after entering number of test cases cursor again in state of asking input user. don't know after giving number of test cases input why asking input . grateful?
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i,n; char * arr[n]; printf("enter number of test cases\n"); scanf("%d \n",&n); printf("enter string\n"); for(i=0;i<n;i++) { arr[i]= (char *)malloc(100*sizeof(char)); scanf(" %s ",arr[i]); } for(i=0;i<n;i++) { printf("the enter string : %s \n",arr[i]); } for(i=0;i<n;i++) { free(arr[i]); } return 0; }
thanks friends discussing question. got answer , real problem scanf function. got nice link read problem. hope it. http://c-faq.com/stdio/gets_flush2.html
the problem in scanf
statement
scanf("%d \n",&n);
please remove ' '
, '\n'
, work
scanf("%d",&n);
also,
char * arr[n];
does not work way want. better solution be.
char **arr = null; /* input n */ arr = malloc(n * sizeof(char *));
Comments
Post a Comment