Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

string error

  • 26-02-2003 3:30pm
    #1
    Moderators, Arts Moderators, Recreation & Hobbies Moderators, Sports Moderators Posts: 9,588 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,588 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