1-I want to know why we put constant in this line(char* dest,const char*src) although if we dont put constant no change, 2-char*strcpy then return strcpy(str1,str2);why we not return char*strcpy(str1,str2);
thankx alot

> why we put constant in this line strcpy( char* dest, const char* src )

because the source of the copy need not be a sequence of mutable characters. without this the following would not be possible:

const char* literal_cstr = "abcd" ;
char dest[128] ;
strcpy( dest, literal_cstr) ;

adding the const is just being const-correct. const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable. The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.

The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable. Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what he or she can do.

In addition, a member function  of a class can be declared as const. In this case, the 'this' pointer is of "const ThisClass* const" type rather than of "ThisClass* const" type. This means that the member function can not modify the object on which it is invoked; member variables can not be modified by it, and non-const member functions which could modify the object can not be called by it.

see: http://parashift.com/c++-faq-lite/const-correctness.html

> 2-char*strcpy then return strcpy(str1,str2);why we not return char*strcpy(str1,str2);

i do not understand the question


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2017 About.com. All rights reserved.