c - Assignment makes pointer from integer without cast -


coming java background i'm learning c, find vague compiler error messages increasingly frustrating. here's code:

/*  * purpose  *      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 //      method *will* modify input arrays. int comparestring(char cstring1[], char cstring2[]) {     // lowercase     cstring1 = strtolower(cstring1);     cstring2 = strtolower(cstring2);      // regular strcmp     return strcmp(cstring1, cstring2); }  // watch out //      method *will* modify input arrays. char strtolower(char cstring[]) {     // declarations     int iteller;      (iteller = 0; cstring[iteller] != '\0'; iteller++)         cstring[iteller] = (char)tolower(cstring[iteller]);      return cstring; } 

this generates 2 warnings.

  • assignment makes pointer integer without cast
    • cstring1 = strtolower(cstring1);
    • cstring2 = strtolower(cstring2);
  • return makes integer pointer without cast
    • return cstring;

can explain these warnings?

c strings not java strings. they're arrays of characters.

you getting error because strtolower returns char. char form of integer in c. assigning char[] pointer. hence "converting integer pointer".

your strtolower makes changes in place, there no reason return anything, not char. should "return" void, or char*.

on call strtolower, there no need assignment, passing memory address cstring1.

in experience, strings in c hardest part learn coming java/c# background c. people can along memory allocation (since in java allocate arrays). if eventual goal c++ , not c, may prefer focus less on c strings, make sure understand basics, , use c++ string stl.


Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -

openssl - Load PKCS#8 binary key into Ruby -