#include <stdio.h>
#include <stdlib.h>
struct test
{
int id;
char name[20];
};
int main()
{
struct test t1;
t1.id=1;
fflush(stdin);
fgets(t1.name,20,stdin);
print((&t1.name));
print1(t1.id,&(t1.name));
}
void print(struct test *name)
{
puts(name);
}
void print1(struct test id,struct test *name)
{
printf("\n%d\n",id);
puts(name);
}
When I run this program it asks for input
test[enter]
output comes out
test 1 (then program terminates)
Why the first puts worked and why puts in second function did not? Yes, there is an option to send complete structure, but I want to know what's wrong here.
Your program does not work for several reasons:
void print1(int id, char *name)
or you should pass the whole structure, by value or by pointer, i.e. void print1(struct test t)
Once you fix these two problems, and make sure that your program compiles warning-free, with all compiler warnings enabled, the issue should be solved.