Basic tips for c++
NULL vs NIL
Each language has its own identifier for no object. In C the standard library, NULL is a typedef of ((void )0). In C++ the standard library, NULL is a typedef of 0 or 0L. You should never use 0 in place of NULL, as it helps the readability of the code. sizeof(NULL) should be the same as sizeof(void).
NULL is an integer in C++, but not necessarily specifically int. NULL can be defined as OL, which would mean that sizeof(NULL) == sizeof(long) in that case.
NIL is an object with a value that tells the programmer the object is not valid. It’s particularly useful in C++ where NULL is defined as 0.
Header for compile
#ifndef __FIX_CVT_H__
#define __FIX_CVT_H__
#ifdef __cplusplus
extern "C" {
#endif
extern ...
#ifdef __cplusplus
}
#endif
#endif /* __FIX_CVT_H__ */
Character type conversion
// char* ---> string
char *cstr = "cstr";
string cppstr = cstr;
cout << "char* ---> string : " < cppstr << endl << endl;
string str;
char cstr[1024];
str = (string)cstr;
// string ---> char*
string cppstr = "cppstr";
const char * cstr = cppstr.c_str();
cout << "string ---> char* : " < cstr << endl << endl;
string str;
char cstr[1024];
strcpy(cstr, str.c_str());
std::string data() vs c_str()
In C++11 onwards, both functions are required to be the same. i.e. data is now required to be null-terminated. According to cpp reference: “The returned array is null-terminated, that is, data() and c_str() perform the same function.”
Leave a comment