C Programming - Structures, Unions, Enums - Discussion
Discussion Forum : Structures, Unions, Enums - Point Out Errors (Q.No. 6)
6.
Point out the error in the program?
#include<stdio.h>
#include<string.h>
void modify(struct emp*);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
Answer: Option
Explanation:
The struct emp is mentioned in the prototype of the function modify() before declaring the structure.To solve this problem declare struct emp before the modify() prototype.
Discussion:
16 comments Page 1 of 2.
Ketaki said:
7 years ago
Answer is 37, no error occur in this.
Gita said:
8 years ago
Yeah correct output is.
Sanjay 37.
Compiled on devc++.
Sanjay 37.
Compiled on devc++.
(1)
Meena said:
9 years ago
Can we pass the address of structure object? Like done in modify(&e).
Shalivahana nandish said:
9 years ago
Yes, op absolutely sanjay 37, in GCC compiler doesn't shows the error, in turbo c it shows the error.
Raji said:
9 years ago
Output coming as Sanjay 37.
No errors.
No errors.
Dhiraj said:
1 decade ago
Here because giving typedef it is taking firstly the structure declaration. After then we are declaring above modify function like: void modify(q*p). So we are getting correct o/p.
Mounika said:
1 decade ago
#include<stdio.h>
#include<string.h>
typedef struct emp q;
void modify(q *p);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
I am not getting any error, and got correct output by using typedef
can anyone explain?
#include<string.h>
typedef struct emp q;
void modify(q *p);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
I am not getting any error, and got correct output by using typedef
can anyone explain?
Artatrana said:
1 decade ago
I agree with rahul there is no error while compiling or executing the program.
Priya said:
1 decade ago
What is meant by prototype?
Surbhi said:
1 decade ago
You need to put the statement "void modify(struct emp*);" after declaring struct because the prototype tells the compiler that there is a struct which is mentioned before hence leading to an error. So prog will look like this:
#include<stdio.h>
#include<string.h>
struct emp
{
char name[20];
int age;
};
void modify(struct emp*);
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
#include<stdio.h>
#include<string.h>
struct emp
{
char name[20];
int age;
};
void modify(struct emp*);
int main()
{
struct emp e = {"Sanjay", 35};
modify(&e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p ->age=p->age+2;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers