Coming from a Java background I'm learning C, but I find those vague compiler error messages increasingly frustrating. Here's my code:
/*
* PURPOSE
* Do case-insensetive string comparison.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int compareString(char cString1[], char cString2[]);
char strToLower(char cString[]);
int main() {
// Declarations
char cString1[50], cString2[50];
int isEqual;
// Input
puts("Enter string 1: ");
gets(cString1);
puts("Enter string 2: ");
gets(cString2);
// Call
isEqual = compareString(cString1, cString2);
if (isEqual == 0)
printf("Equal!\n");
else
printf("Not equal!\n");
return 0;
}
// WATCH OUT
// This method *will* modify its input arrays.
int compareString(char cString1[], char cString2[]) {
// To lowercase
cString1 = strToLower(cString1);
cString2 = strToLower(cString2);
// Do regular strcmp
return strcmp(cString1, cString2);
}
// WATCH OUT
// This method *will* modify its input arrays.
char strToLower(char cString[]) {
// Declarations
int iTeller;
for (iTeller = 0; cString[iTeller] != '\0'; iTeller++)
cString[iTeller] = (char)tolower(cString[iTeller]);
return cString;
}
This generates two warnings.
- assignment makes pointer from integer without a cast
- cString1 = strToLower(cString1);
- cString2 = strToLower(cString2);
- return makes integer from pointer without a cast
- return cString;
Can someone explain these warnings?
You don't need these two assigments:
you are modifying the strings in place.
Warnings are because you are returning a char, and assigning to a char[] (which is equivalent to char*)
C strings are not anything like Java strings. They're essentially arrays of characters.
You are getting the error because strToLower returns a char. A char is a form of integer in C. You are assigning it into a char[] which is a pointer. Hence "converting integer to pointer".
Your strToLower makes all its changes in place, there is no reason for it to return anything, especially not a char. You should "return" void, or a char*.
On the call to strToLower, there is also no need for assignment, you are essentially just passing the memory address for cString1.
In my experience, Strings in C are the hardest part to learn for anyone coming from Java/C# background back to C. People can get along with memory allocation (since even in Java you often allocate arrays). If your eventual goal is C++ and not C, you may prefer to focus less on C strings, make sure you understand the basics, and just use the C++ string from STL.
strToLower should return a char * instead of a char. Something like this would do.
You are returning char, and not char*, which is the pointer to the first character of an array.
If you want to return a new character array instead of doing in-place modification, you can ask for an already allocated pointer (char*) as parameter or an uninitialized pointer. In this last case you must allocate the proper number of characters for new string and remember that in C parameters as passed by value ALWAYS, so you must use char** as parameter in the case of array allocated internally by function. Of course, the caller must free that pointer later.
1) Don't use
gets
! You're introducing a buffer-overflow vulnerability. Usefgets(..., stdin)
instead.2) In
strToLower
you're returning achar
instead of achar
-array. Either returnchar*
as Autopulated suggested, or just returnvoid
since you're modifying the input anyway. As a result, just writestrcasecmp
(Linux & Mac) orstricmp
(Windows).strToLower's return type should be
char*
notchar
(or it should return nothing at all, since it doesn't re-allocate the string)