C Programming - Const - Discussion
Discussion Forum : Const - Point Out Errors (Q.No. 8)
8.
Point out the error in the program.
#include<stdio.h>
#include<stdlib.h>
int fun(const union employee *e);
union employee
{
char name[15];
int age;
float salary;
};
const union employee e1;
int main()
{
strcpy(e1.name, "A");
fun(&e1);
printf("%s %d %f", e1.name, e1.age, e1.salary);
return 0;
}
int fun(const union employee *e)
{
strcpy((*e).name, "B");
return 0;
}
Discussion:
2 comments Page 1 of 1.
Utkarsh said:
6 years ago
The function prototype is that of a const union, but we are trying to modify the union variables inside the last function. Thus compiler fails in trying to automatically cast const char[15] (constant character array) to char * (variable character array).
Sanjay said:
1 decade ago
I Can't understand how can this pointer work, can any one mention it clearly?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers