Thread: Uncle! - How can I cast a string as int?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    96

    Uncle! - How can I cast a string as int?

    I've got a char[] pointer:
    How do I convert the "43" to an integer 43?

    I need x to equal the integer that the string represents.

    I keep getting the location or the ascii value or some other number that doesn't make sense to me.

    I've tried every use of * that I could think of.

    Code:
    int main(int argc, char *argv[])
    {
    	int i=0;
    	int x;
    	char *token[4];
    	token[0] = "43";
    	token[1] = "72";
    	token[2] = "17";
    	token[3] = "9";
    	
    	for(i=0;i<4;i++)
    	{
    		x = (int)(*token)[i];
    		printf("\n%d", x);
    	}
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sscanf?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    quzah - thanks but if it can do it, I sure can't figure out how.


    -----------
    QUOTE "Hundreds of thousands of dip........s can't be wrong."
    Oh yes they can

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    This is a *great* forum!
    I finally found it in an older thread
    atoi

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You should use strtol for better error handling.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    96
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM