This is a piece of code from some book. According to book, this code must not have worked and should have given a error message saying "cannot convert parameter 1 from 'const char[15]' to 'char*'"
but this code is running on turbo C giving just a error message of const to * conversion ????? please tell me whether this code is right ??????
and also try to explain that what does the keyword const means when it is used with enum. What actually becomes constant when we use const with the union variable ??????
Thanks in advance :)

#include<stdio.h>
#include<conio.h>
#include<string.h>

int fun (const union employee *e);
union employee
{
	char name[15];
	int age;
	float salary;
};
const union employee e1;

void main()
{
	clrscr();
	strcpy(&e1.name,"A");
	fun(&e1);
	printf("%s %d %f",e1.name,e1.age,e1.salary);
	getch();

}

int fun(const union employee *e)
{
	strcpy((*e).name,"B");
	return 0;
}

This is a piece of code from some book.

Is that the same book Narue told you to burn ?

int fun (const union employee *e);
union employee
{
char name[15];
int age;
float salary;
};

You should declare the union first and then the function 'fun'.

union employee{
char name[15];
int age;
float salary;
};
int fun (const union employee* );//in function prototypes, you only mention the argument type
void main()//so wrong

According to the standards, main() returns an int type

int main(void)

but this code is running on turbo C giving just a error message of const to * conversion ????? please tell me whether this code is right ??????

How could that be?If the compiler issues an error message then the code should not have compiled in the first place.And yes the code is wrong!If you have used 'const' Type qualifiers then that means that you're declaring them as constants and they should not be modified during runtime.
Quote drom c-faq.com

A const value is one you promise not to modify. The compiler may therefore be able to make certain optimizations, such as placing a const-qualified variable in read-only memory. However, a const-qualified variable is not a true constant; that is, it does not qualify as a constant expression which C requires in certain situations, such as array dimensions, case labels (see section 18.3.1 below), and initializers for variables with static duration (globals and static locals).

Quote from linux man page

const is used to tell the compiler that the data is to be 'read-only'. It is used to help make it easier for the compiler to make certain transformations, or to help the compiler check for incorrect usage of those variables.

For example, the const keyword is commonly used in many functions as a modifier on the parameter type. This tells the compiler that the function will only use the parameter as read-only and will not modify the contents of the parameter variable.

similarly, enum is another keyword used when declaring enumerated data types.

please tell me whether this code is right ??????

No, we've already gone over this. The author of your book is a retard, and you can't trust any of the code in it. Actually, you can trust that the code is probably wrong in all examples.

I can only imagine what that example is trying to teach, but it fails to understand the basic concept of a const contract. When you declare an object as const, you're telling the compiler that it won't be modified. If you then go and try to modify it, that's a breach of contract that will either fail to compile, or if you use gratuitous casts to force the operation, probably fail to execute successfully.

Let me reiterate: your book is shit. Get a new one. Otherwise the process of learning C will be ten times longer because you'll have to unlearn all of the bad habits this book will teach you.

This book is written by Yashwant Kanetkar ;).......although would be buying a new book........ :)......anyways thanks ...........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.