0
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
        
    4. int main(){
    
    5.  int n;
    6.  int i;
    
    7.  printf("Enter size of array : ");
    8.  scanf("%d", &n);
    
    9.  char *arr = (char *)malloc(n*sizeof(char));
    
   10.  printf("Enter string : ");
   11.  gets(arr);
    
   12.  for(i=strlen(arr)-1; i>=0; i--){
   13.    printf("%c", *(arr+i));
   14.  }

   15.  return 0;
   16. }

When i'm trying to execute this code it's only ask for Enter size of array(7th line). After hiting the enter key, this program ends. It does not execute 10th line. It prints 'enter string :' in line 10. but it also ends the program with that. Therefore i cant insert elements into my array. But if i use getchar() after scanf() function in line 8, it works fine. Please explain the reason for this problem.

Thank you so much !

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • 1
    You shouldn't use `gets()`, which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. – MikeCAT Jul 09 '20 at 15:37
  • @AjayBrahmakshatriya: You have remove line numbers from the code. While generally this is a good fix, given question post **refers** to that numbers. Please, do not make edits which **harms** a question. – Tsyvarev Jul 10 '20 at 15:39
  • @Tsyvarev my understanding was that the lines referenced in the text are pretty obvious. But okay, I can revert the edits. – Ajay Brahmakshatriya Jul 10 '20 at 16:43

0 Answers0