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

This is a discussion on invalid conversion from `const char*' to `char' within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem with using character variables. I'm trying to make a char variable equal a string like "hello" ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    22

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

    I'm having a problem with using character variables. I'm trying to make a char variable equal a string like "hello" but still be able to change it.

    Code:
    char arp;
    int pra = 1;
    
    if( pra == 1 ){
        arp = "hello";
    }else
        arp = "bye";
    }
    Also i've tried this...

    Code:
    char arp[20];
    int pra = 1;
    
    if( pra == 1 ){
        arp = "hello";
    }else
        arp = "bye";
    }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    your in c++ dude, use strings,


    Code:
    string arp;
    int pra = 1;
    
    if( pra == 1 )
    {
        arp = "hello";
    }
    else
    {
        arp = "bye";
    }
    dont forget to include <string>
    also brackets are your friends

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    22
    Oh lol...um good ideal. Thanks .

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Explanation:

    1)
    Code:
    char arp;
    int pra = 1;
    
    if( pra == 1 ){
        arp = "hello";
    }else
        arp = "bye";
    }
    The line:
    Code:
    char arp;
    declares a char variable. A char variable can hold a single character, e.g.
    Code:
    char arp;
    arp = 'a';
    ...and a character in C++ is enclosed with single quotes--not double quotes. A string literal like:

    "hello"

    cannot be a char type.

    2)
    Code:
    char arp[20];
    int pra = 1;
    
    if( pra == 1 ){
        arp = "hello";
    }else
        arp = "bye";
    }
    First, your missing an opening bracket for your else branch. Then, in this line:

    char arp[20];

    you are declaring an array of characters. Generally, you cannot assign a string literal like "hello" to an array of characters. However, there is one little twist: with character arrays, you are able to assign a string literal to them at the time you declare them. For instance, you can do this:

    char arp[20] = "hello";

    When you do that the compiler will copy "hello" into the arp array character by character, and fill the remaining index positions with '\0' characters. You cannot subsequently do this, though:

    arp = "bye";

    because that doesn't fit the requirements: you are not assigning the string literal to arp at the time you declare arp. arp has already been declared.
    Last edited by 7stud; 07-31-2005 at 06:30 PM.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, I don't know how hackish this is, but:
    Code:
    	char* arp = new char[20];
    	int pra = 1;
    	if( pra == 1 ){ arp = "hello"; }
    	else  { arp = "bye"; }
    That is if you still want to use this. In this way, only the memory addresses of "ehllo" or "bye" are assigned to the arp* pointer, and it all works. However, std::string is overpoweringly great, so whatev. I don't know the exact reason why you can't assign things like you did, I can't really think of how it would look on a low level...

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes, very hackish.
    Use the strcpy function in the <cstring> header.
    Code:
    strcpy(arp, "hello");
    Code:
    arp = "hello";
    This is pointer assignment, and this is why you could not get it to work correctly. When you declare an array statically, it has a fixed position, and your named variable will always point to that location until it the array goes out of scope, and gets reclaimed by the system. What the above code tries to do is assign 'arp' to point to the string literal "hello" which is probably somewhere in read-only memory, but since you cannot reassign this pointer, you get an error.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by Tonto
    Well, I don't know how hackish this is, but:
    Code:
    	char* arp = new char[20];
    	int pra = 1;
    	if( pra == 1 ){ arp = "hello"; }
    	else  { arp = "bye"; }
    That is if you still want to use this. In this way, only the memory addresses of "ehllo" or "bye" are assigned to the arp* pointer, and it all works. However, std::string is overpoweringly great, so whatev. I don't know the exact reason why you can't assign things like you did, I can't really think of how it would look on a low level...
    No, no, no, no. That's bad. You've just created a memory leak by reassinging the address stored in the pointer from the memory you allocated initially using the new operator to a different address (the address of one of the string literals). Bad things could happen if you were to attempt to call delete on the pointer. If you don't belive me run this:

    Code:
    char * arp = new char[20];
    
    cout << "Address stored in arp: " << hex << reinterpret_cast<int>(arp) << endl;
    arp = "hello";
    cout << "Address stored in arp: " << hex << reinterpret_cast<int>(arp) << endl;
    On my machine I get this as output:
    Code:
    Address stored in arp: 320b50
    Address stored in arp: 4170b0
    As you can see, you lose the address of the memory you've initially allocated using the new operator.
    Last edited by hk_mp5kpdw; 08-01-2005 at 10:14 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid conversion from `char*' to `char'
    By Ron in forum C Programming
    Replies: 11
    Last Post: 06-14-2008, 03:10 PM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. invalid conversion from `char*' to `char'
    By Tommy7 in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2005, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21