How can I make my strchr function take both 'const char' and 'char' arrays as first parameter?

Related searches

I am expecting my function to work just the same as the function "strchr" I am using from the cstring / string.h library. I understand that I cannot cast a " const char* " variable / array to " char* ". Yet, how is the predefined "strchr" function able to be passed both "const char" and "char" data type arrays and work just fine ? How could I change mine so that it works, too ?

char* my_strchr(char *place_to_find, char what_to_find)
for(int i=0;place_to_find[i];i++)
    if(what_to_find==place_to_find[i]) return place_to_find+i;
return NULL;
int main()
const char vocals1[]="AEIOUaeiou";
char vocals2[]="AEIOUaeiou";
return 0;

As you could probably already tell, my third cout<< does not work. I am looking for a way to change my function ( I am guessing the first parameter should somehow be typecast ).

How can I make my strchr function take both 'const char' and 'char' arrays as first parameter?

You could change the argument to be const char*. That would allow passing both pointers to char as well as const char.

However, you return a non-const pointer to that array, which you shouldn't do if the pointer is to a const array. So, when passing a const char*, your function should also return const char*

Yet, how is the predefined "strchr" function able to be passed both "const char" and "char" data type arrays and work just fine ?

There are two predefined std::strchr functions. One that accepts and returns char* and another that accepts and returns const char*:

const char* strchr(const char* str, int ch);
      char* strchr(      char* str, int ch);

If you would wish to return char* in case of char* argument, you need to have a different function for each case, like the standard library has. You can use an overload like the standard library does, or you can use a function template to generate both variations without repetition:

template<class Char>
Char* my_strchr(Char *place_to_find, char what_to_find)

Note that the C version of the function is declared char *strchr(const char *str, int ch). This makes the single function usable in both cases, but is unsafe, since the type system won't be able to prevent the user of the function from modifying though the returned non-const pointer even when a const array was passed as an argument.

C strchr() Function with example, In this example, we have a string and we are searching a character 'u' in the string using strchr() function. When we displayed the string value of the returned� Following is the declaration for strchr() function. char *strchr(const char *str, int c) Parameters. str − This is the C string to be scanned. c − This is the character to be searched in str. Return Value. This returns a pointer to the first occurrence of the character c in the string str, or NULL if the character is not found. Example. The

Make two overloads, the way it is done in the C++ standard library:

char*       my_strchr(      char *place_to_find, char what_to_find)
const char* my_strchr(const char *place_to_find, char what_to_find)

Even though in your case only the second overload would be sufficient (demo) you would not be able to support an important use case, when you need to find a character and then replace it:

// This would not work with only one overload:
char *ptr = my_strchr(vocals2,'e');
if (ptr) {
    *ptr = 'E';

That is why the non-const overload is necessary.

Note: I assume that you are doing this as a learning exercise, because C-style string functions are no longer necessary for new development, having been replaced with std::string functionality.

strchr() function in C++ and its applications, Returns pointer to the first occurrence // of c in str[] char *strchr(const char *str, int c) Pass the given string in the strchr() function and mention the Get hold of all the important DSA concepts with the DSA Self Paced We use cookies to ensure you have the best browsing experience on our website. The function strchr() searches the occurrence of a specified character in the given string and returns the pointer to it. C strchr() Function char *strchr(const char *str, int ch) str – The string in which the character is searched. ch – The character that is searched in the string str. Return Value of strchr()

Short answer: make the type of the place_to_find const char*

The reason for your error is that you cannot implicitly convert a pointer to a const char to a pointer to a non-const char. If you could, then you could change the char that the pointer points to and it would defeat the purpose of having it a const char type in the first place.

You can implicitly convert a pointer to a non-const char to a pointer to a const char because it does not remove any restrictions.

L.E.: Also, the return value should be a const char*, because again, if you don't, it would remove the const restriction which is not allowed. The only problem with that is that you would not be able to modify the array through the pointer returned. If you also want that, then you would have to overload the method on both char and const char.

strchr() function in C | C String, strchr() function in C:strchr( ) function returns pointer to the first occurrence of the occurrence of a character in a given string, you can use below C program. C. BUILT IN STRCHR FUNCTION. Header File: string.h. To clearly visualize how the stringchar function works let’s take the help of an online c visualizer (treats both upper and lower case

strchr, wcschr, _mbschr, _mbschr_l, The strchr function finds the first occurrence of c in str, or it returns NULL if c is not found. sets errno to EINVAL. strchr and wcschr do not validate their parameters . In C, these functions take a const pointer for the first argument. If you require the non- const behavior for both C++ overloads, define the� strchr() prototype const char* strchr( const char* str, int ch ); char* strchr( char* str, int ch ); The strchr() function takes two arguments: str and ch. It searches for the character ch in the string pointed to by str. It is defined in <cstring> header file. strchr() Parameters. ptr: Pointer to the null terminated string to be searched for.

[PDF] The strchr() function finds the first occurrence of a specified , The function strchr() searches str from left to right until the character ch is found or the terminating Using character test macros to create an integer */ We commonly use one-dimensional array for standard use and two-dimensional array to. strch() function can also be used to check the presence of a character in a strinG. The input consists of a character we want to check, if it exists in the string. For example – Lets check if the characters A and z are present in the string – “My name is Ayush”

f function strchr C String function – Strrchr char *strrchr(char *str, int ch) It is similar to the function strchr, the only difference is that it searches the string in reverse order, now you would have understood why we have extra r in strrchr, yes you guessed it correct, it is for reverse only. Now let’s take the same above example:

  • If you don't edit the values, you should accept and return const char* types instead. Then when provided with a char*, it will implicitly cast it to a const char*
  • As you can see from doc, there are 2 overloads. (and you have several ways to factorize implementation).
  • As Kyle said, if your function is not going to modify the value, you should always use const char*. They it is OK to pass char* to it. If your function is going to modify the value, then the const version should not apply.
  • @KyleWillmon -- your comment is correct, except that "implicitly cast" should be "implicitly convert". A cast is something you write in your source code to tell the compiler to do a conversion. There is no such thing as an implicit cast.
  • @PeteBecker Thanks. I can no longer edit it, but I'll try to use the correct language in the future. I also have since realized that two overloads is a better solutions since, in the case of a mutable string, you may want to mutate the string using the returned pointer (so the return when given a char* should be a char* rather than a const char*)
  • restriction comes from return value.