Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

const question

Status
Not open for further replies.

NiceButDim

Programmer
Feb 12, 2002
154
0
0
GB
I would be grateful if someone could answer what is a very basic, and hopefully simple, question;

The prototype of the strstr function is
char *strstr(const char *haystack, const char *needle);

What is the point of the const keyword in this case? why is the prototype not simply;
char *strstr(char *haystack, char *needle);

Thanks.

john.
 
If the syntax is:
Code:
char *strstr(const char *haystack, const char *needle);
then you can rest assured that the two strings you pass to strstr() won't be changed by strstr().
Without the const infront of the parameters, you can't guarantee if the strings will be modified or not.

Now obviously, a function could lie and use a cast to remove the constness of the variables; then they could do whatever they want with it. But if they did that, the person that wrote it should be fired.
 
Thank you both for the replies.

It was a dumb question now that I think of it, but I haven't worked with C for a few years and someone just asked me the question and my mind went blank!

Thanks again.

john.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top