dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Intermediate
May 31, 2000

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Differences Between C and C++ in the Prototypes of Standard Functions

A small number of functions from the Standard Library have different signatures on C and C++. These functions are: strchr(), strpbrk(), strrchr(), strstr(), and memchr() as well as their wide-character counterparts: wcschr(), wcspbrk(), wcsrchr(), wcsstr(), wmemchr(). They are declared in the standard header Let's look at strstr() for example.

While in C strstr() has the following prototype:

 
 char *  strstr(const char*s1, const char *s2);

In C++, this function has two different prototypes that are incompatible with the C version:

 
 char * strstr(char *s1, const char * s2); 
 const char * strstr(const char * s1, const char *s2); 

Let's look at another example: the function strpbrk(). In C, it has the following signature:

 
 char * strpbrk(const char *s1, const char *s2); /*ANSI C*/

In C++, it has two different signatures:

 
 char *  strpbrk(char *s1, const char *s2);  
 const char * strpbrk(const char *s1, const char *s2);

Can you see a pattern here? The C version takes "const X *""as an argument and returns "X *", whereas C++ defines two versions: one taking "X *" as an argument and returning "X *", and another taking "const X *" as an argument and returning "X *". In C++ you have better type-safety, which is feasible thanks to function overloading.

You should pay attention to these subtle differences between C and C++ when you port code from C to C++.

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date