Reputation: 551
Below is the code through which I am returning structure from function. May be the question is very basic but I am not clear why I am writing emp
in this line struct emp putinfo (){
. I am not clear why I need to write the structure name? Because inside the function I am defining the structure as struct emp t;
. What is the specific reason?
#include <stdio.h>
struct emp {
int number;
char name [200];
};
void print (struct emp team){
printf ("Details of employee %d %s", team.number, team.name );
}
struct emp putinfo (){
struct emp t;
printf ("Enter number"); scanf ("%d", &t.number);
printf ("Enter name"); scanf ("%s", &t.name);
return t;
}
int main (){
struct emp test;
test = putinfo ();
print (test);
return 0;
}
Upvotes: 1
Views: 213
Reputation: 5543
You're right in that this is redundant (at a first glance, but see below), but this is how C works, there is no type inference. This is the same for every type (and unrelated to structures):
succ.c
unsigned succ(unsigned n) {
unsigned succ = n + 1;
return succ;
}
Here, you could also say, there's no real reason you have to specify the return type.
So, now the part why this is needed:
1) It's needed at least in a prototype because the compiler may not be able to see the definition, consider
main.c
#include <stdio.h>
unsigned succ(unsigned);
int main(void) {
printf("%u\n", succ(1));
}
where the object file containing the implementation of succ
may be already compiled (you could compile via gcc main.c succ.o
or the like).
2) Consider
long int_to_long(int n) { return n; }
where an int
is converted to a long
(and the compiler couldn't have known without a return type). OK, this doesn't hold for structures (they are never implicitly converted), but it's consistent.
And usually, you don't pass structures by value at all, but pass pointers to it, especially if they are large.
Finally, a few notes about your code:
Never, ever, use %s
without a length for scanf
. It's always wrong (how should scanf
know how big the array you provided is?), do e.g.
printf ("Enter name\n"); scanf ("%199s", t.name);
or use fgets
instead; in productive code, you almost never want to use scanf
, because the error handling is complicated. Also note the newline I added, the message asking for input may not appear until after you've written a newline to stdout
(stdout
is usually line buffered, and so is usually a terminal, a (better) alternative would be fprintf(stderr, "Enter name: ");
, as stderr
is unbuffered and this output shouldn't really go to stdout
anyway.).
Generally, I would advise you to not write interactive programs until you're familiar with pointers and arrays, dynamic memory allocation, error handling, and file buffering, but pass information from the user via command-line arguments. You currently know too little to write interactive programs properly.
HTH
Upvotes: 0
Reputation: 14360
In C
the name of the type you create when you define a structure is struct structure_name
C++
allows to use this format or just structure_name
Example:
struct some_struct{};
struct some_struct x; // Variable declaration C, C++ syntax.
some_struct y; // Only C++ syntax.
If you are working with C
you must specify the return value of your function using C
syntax:
struct some_struct my_function(){}
on the other hand in C++
you can
struct some_struct my_function(){}
or
some_struct my_function(){}
Upvotes: 1
Reputation: 3858
Define your struct and use the type definition instead:
#include <stdio.h>
typedef struct {
int number;
char name [200];
}emp;
void print (emp team){
printf ("Details of employee %d %s", team.number, team.name );
}
emp putinfo (){
emp t;
printf ("Enter number"); scanf ("%d", &t.number);
printf ("Enter name"); scanf ("%d", &t.name);
return t;
}
int main (){
printf("hello\n");
emp test;
test = putinfo ();
print (test);
return 0;
}
Upvotes: 0
Reputation: 1409
struct emp
is actually the name with which you address the structure in C. You can typedef
it and then you can write only emp
.The reason why it stands before putinfo
is because this is the returned type by the function.struct emp t
just makes a new structure of type emp
and assigns it to t
.
Upvotes: 1