Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

string error

  • 26-02-2003 04:30PM
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,760 Mod ✭✭✭✭


    #include<iostream.h>

    char Elec_Arts[2];

    char value;

    value = strcmp(Elec_Arts, 'a1'); //a1 being a grade result


    I get the errror:

    "m Files\Microsoft Visual Studio\MyProjects\test\test.cpp(21) : error C2664: 'strcmp' : cannot convert parameter 2 from 'const int' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.
    "

    what am I doing wrong??


Comments

  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    You need to use double quotes around the string. Use single quotes for single characters.

    Zab.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    'a1' is an erroneous char literal. Erroneous because it contains 2 characters.

    IIRC the behaviour here is undefined, though it definitely shouldn't be treated as a string. MS behaviour is to treat it as an integer with the value of an 'a' followed by a '1' packed in as bytes (hence it becomes 0x00006131).

    You can't strcmp an int (or for that matter a char).

    What you presumably wanted was the string "a1":
    value = strcmp(Elec_Arts, "a1");
    

    This is an easy mistake to do if you use Java or Javascript (since in those languages ' and " can be used interchangeably).


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by hussey
    you are trying to cast an int to a char

    There's nothing illegal about that, though it is of course unwise (especially if you wanted to know more than whether they matched, since chars are unsigned on some implementations).

    Even if it was only desirable to compare with 0 (to identify a match) chars are slower than ints. ints are generally defined to be the size the most common processors (32bit intels in the case of Windows) handles best (4bytes in the case of 32bit pentiums) and hence are faster than other integer types.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    And that's a warning to hussey about the deletion rule...


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    exactly when can one delete a thread??

    I deleted it as my answer was wrong, didn't think it would damage the flow of the thread


  • Advertisement
  • Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,760 Mod ✭✭✭✭BossArky


    thanks Zab, the double quotes worked.


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Yep. No worries hussey. It did indeed do no damage.

    I was, for completely unconnected reasons, in paranoia mode at the time.

    Apologies.


Advertisement