The return type
Don't use char
as the return type in
char findInStr(char *str, char *find);
On many (and on all POSIX compliant) platforms, char
is a 8-bit integer
which may be unsigned or signed, so the maximal return value would be 255
or 127
.
Another problem is that you count the number of occurrences
in an unsigned char
, which means that – for example – a count of
200
would be returned as -56
if the character type is (8 bit) signed.
A better choice would be int
(which is guaranteed to have at least
16 bits) or long
(at least 32 bits). To be completely on the safe
side, use size_t
, which is a type than can hold the size of
any object.
Const parameters
Since your function does not modify the passed strings, it is a good habit to declare them as constant:
size_t findInStr(const char *str, const char *find);
The compiler can then check that you don't (inadvertently) modify the pointed-to memory, and may be able to do further optimizations on the calling side.
It also documents that the function does not modify the strings.
Array sizes
In
char find[5] = "asd";
the array is one element too large. That does no harm, but there is a risk of forgetting to change the array size if the string on the right-hand side is modified. Better let the compiler determine the size automatically:
char find[] = "asd";
And here
char str[60];
// ...
gets_s(str, 60);
the array size is specified twice, which bears the risk of changing it at one place later but not at the other place. That is avoided with
char str[60];
// ...
gets_s(str, sizeof(str));
The return type
Don't use char
as the return type in
char findInStr(char *str, char *find);
On many (and on all POSIX compliant) platforms, char
is a 8-bit integer
which may be unsigned or signed, so the maximal return value would be 255
or 127
.
Another problem is that you count the number of occurrences
in an unsigned char
, which means that – for example – a count of
200
would be returned as -56
if the character type is (8 bit) signed.
A better choice would be int
(which is guaranteed to have at least
16 bits) or long
(at least 32 bits). To be completely on the safe
side, use size_t
, which is a type than can hold the size of
any object.
Const parameters
Since your function does not modify the passed strings, it is a good habit to declare them as constant:
size_t findInStr(const char *str, const char *find);
The compiler can then check that you don't (inadvertently) modify the pointed-to memory, and may be able to do further optimizations on the calling side.
It also documents that the function does not modify the strings.
The return type
Don't use char
as the return type in
char findInStr(char *str, char *find);
On many (and on all POSIX compliant) platforms, char
is a 8-bit integer
which may be unsigned or signed, so the maximal return value would be 255
or 127
.
Another problem is that you count the number of occurrences
in an unsigned char
, which means that – for example – a count of
200
would be returned as -56
if the character type is (8 bit) signed.
A better choice would be int
(which is guaranteed to have at least
16 bits) or long
(at least 32 bits). To be completely on the safe
side, use size_t
, which is a type than can hold the size of
any object.
Const parameters
Since your function does not modify the passed strings, it is a good habit to declare them as constant:
size_t findInStr(const char *str, const char *find);
The compiler can then check that you don't (inadvertently) modify the pointed-to memory, and may be able to do further optimizations on the calling side.
It also documents that the function does not modify the strings.
Array sizes
In
char find[5] = "asd";
the array is one element too large. That does no harm, but there is a risk of forgetting to change the array size if the string on the right-hand side is modified. Better let the compiler determine the size automatically:
char find[] = "asd";
And here
char str[60];
// ...
gets_s(str, 60);
the array size is specified twice, which bears the risk of changing it at one place later but not at the other place. That is avoided with
char str[60];
// ...
gets_s(str, sizeof(str));
The return type
Don't use char
as the return type in
char findInStr(char *str, char *find);
On many (and on all POSIX compliant) platforms, char
is a 8-bit integer
which may be unsigned or signed, so the maximal return value would be 255
or 127
.
Another problem is that you count the number of occurrences
in an unsigned char
, which means that – for example – a count of
200
would be returned as -56
if the character type is (8 bit) signed.
A better choice would be int
(which is guaranteed to have at least
16 bits) or long
(at least 32 bits). To be completely on the safe
side, use size_t
, which is a type than can hold the size of
any object.
Const parameters
Since your function does not modify the passed strings, it is a good habit to declare them as constant:
size_t findInStr(const char *str, const char *find);
The compiler can then check that you don't (inadvertently) modify the pointed-to memory, and may be able to do further optimizations on the calling side.
It also documents that the function does not modify the strings.