Why we can't use arrow operator in gets and puts?


You can't read user input in an un-initialized pointer. Instead, have a variable of the struct data type and assign its address to pointer before accessing its inner elements by → operator

example

#include <stdio.h>
struct example{
   char name[20];
};
main(){
   struct example *ptr;
   struct example e;
   puts("enter name");
   gets(e.name);
   ptr=&e;
   puts(ptr->name);
}

Output

Typical result of above code

enter name Disha
You entered Disha

Updated on: 22-Jun-2020

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements