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: 2020-06-22T09:11:02+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements