My code to read a string into a struct isn't working. I've tried fgets, gets and also scanf, and none of them is working, returning random characters when I run the code. Can anyonye please help me? Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
struct student
{
char string[100];
};
struct student get_detail(int n)
{
struct student wannabe;
if (n==1)
{
printf("String:\n");
scanf("%s", wannabe.string);
//fgets(wannabe.string, 100, stdin) didn't work;
//gets (wannabe.string) also didn't work
//scanf("%s", wannabe.string) didn't work
return wannabe;
}
}
void main()
{
int x = 1;
struct student test = get_detail(x);
printf("\nString:%s", test.string);
}