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.
Gita said:
8 years ago
Yeah correct output is.
Sanjay 37.
Compiled on devc++.
Sanjay 37.
Compiled on devc++.
(1)
Basant Sukhdev said:
1 decade ago
Correct output will be-
sanjay,37
explanation::-all declaration is correct ,like-structure declaration & function declaration(with global scope)
when modify() is called "strupr()" converts "sanjay" into "sanjay"
note:-strupr() is function of <string.h> so it must be included before using it..!!
and p->age=p->age+2;
add 2 in 35 (i.e 35+2=37)
sanjay,37
explanation::-all declaration is correct ,like-structure declaration & function declaration(with global scope)
when modify() is called "strupr()" converts "sanjay" into "sanjay"
note:-strupr() is function of <string.h> so it must be included before using it..!!
and p->age=p->age+2;
add 2 in 35 (i.e 35+2=37)
N n said:
1 decade ago
This looks wrong on many counts.
Struct says: char name[20], and yet "Sanjay" is of type (const char *). You're assigning a pointer to an array?
Struct says: char name[20], and yet "Sanjay" is of type (const char *). You're assigning a pointer to an array?
Pls said:
1 decade ago
Can any one explain clearly?
Khamil said:
1 decade ago
void modify(struct emp*);
has been declared before struct.
if it z declared after struct then the o/p vl be
sanjay 37
has been declared before struct.
if it z declared after struct then the o/p vl be
sanjay 37
Saleem said:
1 decade ago
According to explanation...the compiler can't identify the prototype of "struct emp" at the declaration because it's not defined yet. So compiler can't estimate the memory need & tools.
Rahul Singh said:
1 decade ago
No Error in the program. We are just instructing the compiler about the prototype of function modify. It is the abstraction so no need to estimate the size of struct emp.
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;
}
Priya said:
1 decade ago
What is meant by prototype?
Artatrana said:
1 decade ago
I agree with rahul there is no error while compiling or executing the program.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers