Thread: Converting a char* to int, why is that so hard?!?!

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Converting a char* to int, why is that so hard?!?!

    Code:
    int main()
    {
    char* c = "123456";
    int i = atoi(c[3]);
    //i = (int)c[3]; 
    //i = atoi((char*)c[3]);
    //i = atoi((char)c[3]);
    //char cc = (char)c[3]; i = atoi(cc);
    //none works, what in the... ........ing me off!
    
    cout << i;
    
    return 0;
    }
    Thats basically what I want to do, I want "int i" to have the c[3] number, which is "4"...

    What on Gods green earth am I doing wrong? I mean.... Always getting the error "invalid conversion from char to char*".
    Last edited by ManyTimes; 03-19-2010 at 10:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The error means just what it says: atoi expects a char*; c[3] is a char. They are not the same thing, and can't be used interchangeably. If you just want to convert a single character, then atoi is not for you (a single character is not a string). The traditional way to convert a single character is to subtract '0' from the value of the character.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25

    Unhappy

    Thanks, but next time instead of writing two lines which actually don't answer my question, just write the little "snippet" down instead?
    For instance:
    int i= char[4]-'0';

    Because, I know what you said, even subtracting '0', but how come the teacher included:
    #include <cstdlib> // itoa
    When I don't really need it??? Hm... Maybe I should have said this in the first post, oh... 6AM do that to you!

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Quote Originally Posted by ManyTimes View Post
    Because, I know what you said, even subtracting '0', but how come the teacher included:
    #include <cstdlib> // itoa
    When I don't really need it??? Hm... Maybe I should have said this in the first post, oh... 6AM do that to you!
    He could be jerking you around.
    He could be incompetent.
    But first, are you sure that you've read and understood the assignment correctly?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by ManyTimes View Post
    Thanks, but next time instead of writing two lines which actually don't answer my question, just write the little "snippet" down instead?
    tabstop actually answered your question, as you asked it.

    S/he can't be held responsible if you asked for one thing but wanted another.

    Also, bear in mind, that this site is not a free "do my homework" resource.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Thanks, but next time instead of writing two lines which actually don't answer my question, just write the little "snippet" down instead?
    O_o

    How about next time you not waste everyone's time by posting a question you claim you know the answer to?

    Not that I'm going to help. You've pretty much guaranteed that I'm not going to bother with you.

    Soma

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Two quotes...
    Quote Originally Posted by ManyTimes
    What on Gods green earth am I doing wrong? I mean.... Always getting the error "invalid conversion from char to char*".
    Quote Originally Posted by tabstop View Post
    The error means just what it says: atoi expects a char*; c[3] is a char. They are not the same thing, and can't be used interchangeably. If you just want to convert a single character, then atoi is not for you (a single character is not a string). The traditional way to convert a single character is to subtract '0' from the value of the character.
    You know what the character "?" means? (See, there it is!) It marks the end of a QUESTION. You asked a question what you were doing wrong. Tabstop writes two perfectly clear lines on what you did wrong. And then you complain that he didn't answer your question?
    Be thankful, don't be a prick.

  8. #8
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    ManyTimes, if you asked a question while you wanted to ask another. you
    shouldnt ignore the good answer and blame the helper (tabstop) for something
    you are the responsible, either say thanks and then ask your real question or
    dont ask anything at all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  3. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM