how to use char* to store digit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jia777
    New Member
    • Sep 2006
    • 1

    how to use char* to store digit

    hi i have a question on c++

    can u tell me how we use char* to store digits?????
    and the digits are characters.....
    thx..
    hurry!!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can not use char 8* to store digits directly because it is a pointer to a location that stores digits not a place to store digits.

    You would have to do it indirectly

    Code:
    char *pDigits;
    int i;
    
    pDigits = malloc(11);
    
    for( i=0; i<10; i++)
    {
        pDigits[i] = (char)('0' + i);
    }
    
    pDigits[10] = '\0';
    
    printf("Digits: %s\n", pDigits);

    Comment

    Working...