C|

This is a discussion on C| within the C Programming forums, part of the General Programming Boards category; 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; ...

  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
    21,886
    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';
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,246
    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


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