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.
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;
}
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?
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)
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.
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.
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.
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?
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
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.
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