Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

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

#include
struct example
{
char name[20];
};
main()
{
struct example *ptr;
puts("enter name");
gets(ptr->name);/////why it does not accept this
puts(ptr->name);///it will results segmentation fault
}

With dot operator it's ok to use gets. e.g gets(s.name); but not with arrow operator please reply as soon as possible.


1 Answer
Pythonista

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

#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);
}
Typical result of above code
enter name Disha
You entered Disha
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.