Thread: C|

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Unhappy C|

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main (void) {
    	
    	char wth[1001]="12345678900761235897612486";
    	int wtha;
    	
    	wtha=atoi(&wth[0]);
    	printf("%i", wtha);
    
    	return 0;
    }
    why does this not work?
    i want that wtha becomes 1. - nothing else.
    what can i do?
    why is the output not:
    "1"
    ????
    greetings-mekeor

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mekeor
    why does this not work?
    Because wth when converted to a pointer to its first element is equivalent to &wth[0].

    Quote Originally Posted by Mekeor
    what can i do?
    It sounds like you want to write:
    Code:
    wtha = wth[0] - '0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    hmm

    thanks for your reply,
    but it didn't solve my problem yet.

    i have a number as string and want to convert the first number to a integer called wtha, the second to a integer called wthb.

    nothing else.


    i hope my question is clear now. ?
    thank you

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Did you mean the first digit to wtha and 2nd to wthb?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by Bayint Naung View Post
    Did you mean the first digit to wtha and 2nd to wthb?
    And if he did mean that, laserlight's post included the answer for him.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Tags for this Thread