Thread: invalid conversion from 'const char*' to 'char'

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    invalid conversion from 'const char*' to 'char'

    I'm trying to make a little code that makes a 2-dimensional character array and fills it with 0's(or a's or whatever char) for now.

    it gives me this error when i compile:

    invalid conversion from 'const char*' to 'char'
    here's the code:

    Code:
        int scrsizex=10, scrsizey=10;
        char scr[scrsizey][scrsizex];
        
        for (int county=0; county<scrsizey;county++){
            for (int countx=0; countx<scrsizex;countx++){
                scr[county][countx]="0";
                }
        }
    Compiling with Dev-C++ v4.9.9.2


    Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scr[county][countx]="0";
    Use
    scr[county][countx]=0;
    or
    scr[county][countx]='\0';

    Use the single quotes for letters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    ok, i thought i tried doing that but it didn't work... ok...


    just ran it.
    i think i had tried using ='0' and that's why it didn't work...
    thanks!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually, '0' should work just as well as '\0'. They are two different letters. '0' is the character zero, while '\0' is the null character. If you print out '0', you will get zeros, but if you print out '\0' you will get nothing. In this case it would probably be better to use an actual character like '0', 'a', '*', ' ' (space), etc.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    "0" would actually represent the address of that string and it is complaining because you are trying to assign the address of that string to a char variable when that variable isn't char*.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scr[county][countx]="0";
    You could write

    scr[county][countx]="0"[0];

    Or even
    scr[county][countx]="0"[1];

    /runs away
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int scrsizex=10, scrsizey=10;
    char scr[scrsizey][scrsizex];
    You must use constant sizes, use 'const int' instead of 'int'.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM