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;
}
Error: in structure
Error: in prototype declaration unknown struct emp
No error
None of above
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 2 of 2.

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.

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.

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

Pls said:   1 decade ago
Can any one explain clearly?

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?

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)


Post your comments here:

Your comments will be displayed after verification.