casting a string of chars as a string of ints

This is a discussion on casting a string of chars as a string of ints within the C Programming forums, part of the General Programming Boards category; For a string text , why isn't for(i = 0; strlen = n, i < n; i++) text[i] = (int) ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question casting a string of chars as a string of ints

    For a string text,

    why isn't

    for(i = 0; strlen = n, i < n; i++)
    text[i] = (int) text[i]

    casting each character of text as an integer?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Presumably text is an array of chars, so of course it remains an array of chars. Perhaps you want to copy over each char in text to an int in an array of ints.
    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
    Sep 2009
    Posts
    2

    Arrow

    How do I do that?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    int nums[10];
    char str[10];
    
    for ( int i = 0; i < 10; ++i )
    {
        nums[i] = str[i];
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    don't forget the (int) cast

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Epy View Post
    don't forget the (int) cast
    Why would you need one? I'm assigning a 1 byte int to a larger int, so no cast is needed.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Casting a vector to a string
    By Hikaru in forum C++ Programming
    Replies: 10
    Last Post: 08-21-2006, 07:55 PM
  3. Replies: 8
    Last Post: 03-31-2006, 07:15 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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