convert int to const char *

This is a discussion on convert int to const char * within the C Programming forums, part of the General Programming Boards category; an argument to a function i need to use is a const char *. i would like to convert an ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    convert int to const char *

    an argument to a function i need to use is a const char *. i would like to convert an int to a const char * so that i could pass it to the function. how do i convert an int to a const char *?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The most straightforward method is sprintf:
    Code:
    char buf[256];
    
    sprintf(buf, "%d", value);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    so what do i pass to the function? buf? i don't want to print anything.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so what do i pass to the function? buf?
    sprintf writes to the first argument, so yes, buf would contain the string representation of value.

    >i don't want to print anything.
    This suggests you didn't take the time to understand what sprintf does before replying.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for your replies. For some reason, this method is not working for me. I'll look for help elsewhere.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'll look for help elsewhere.
    How quickly you give up. Maybe if you posted your code, I could help more. But since you're going to look for help "elsewhere", I'll not worry about it anymore. Good luck.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Prelude View Post
    >I'll look for help elsewhere.
    How quickly you give up. Maybe if you posted your code, I could help more. But since you're going to look for help "elsewhere", I'll not worry about it anymore. Good luck.
    Well spoken. I'm surprised you didn't close the thread immediately. I mean, who cares, he'll look for someone to do his homework elsewhere, right?

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by greeney View Post
    so what do i pass to the function? buf? i don't want to print anything.
    sprintf is exactly like printf, except the output is stored in a string, rather than being sent to stdout. The code godess' sprintf call places the integer value in buf as a character string. An alternative would be to use itoa.

    Code:
    #include <stdlib.h>
    /* ... */
    char buf[256] = (itoa(value));

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >An alternative would be to use itoa.
    Not an especially good alternative, seeing as how itoa is not a standard function and the OP's compiler may not support it. sprintf is always available.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Greeney -

    A friendly bit of advice - stop acting like an ass, and pay attention here. You could trip over one of Prelude's replies, fall down, break your foot in two places, and learn a few things about C, before you ever hit the ground.

    You see that blue marble over by her name? Look down about an inch or so, and toward 5 o'clock from the marble. You see that number there? So what do you surmise? Well, nevermind - who wants to listen to an ass, anyway?

    Point is, she has answered your question, but you need to take that last step. You have the char in buff. so how could you make a const char* point to buff[0]?

    Work it through and you'll see.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Because I'm tired of people recommending itoa, and because I'm just so nice, here's a roughly equivalent implementation of itoa that can be used without relying on non-portable extensions to the standard library:
    Code:
    #include <assert.h>
    #include <stdlib.h>
    
    char *cprog_strrev(char *s, size_t n)
    {
        size_t i, j;
    
        for (i = 0, j = n - 1; i < j; i++, j--) {
            char save = s[i];
            s[i] = s[j];
            s[j] = save;
        }
    
        return s;
    }
    
    char *cprog_itos(int value, char *buf, size_t size, int radix)
    {
        const char *digits = "0123456789ABCDEF";
    
        int sign = value < 0;
        size_t n = 0;
    
        /* Required space: sign(opt) + digit + '\0' */
        assert(sign ? size > 2 : size > 1);
    
        if (radix == 0)
            radix = 10; /* Default to decimal */
    
        do
            buf[n++] = digits[abs(value % radix)];
        while (n < size - 1 && (value /= radix) != 0);
    
        if (sign)
            buf[n++] = '-';
    
        buf[n] = '\0';
    
        /* The string is built in reverse; fix it */
        return cprog_strrev(buf, n);
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. Can't Find Conio.h?
    By drdroid in forum C++ Programming
    Replies: 27
    Last Post: 09-26-2002, 04:24 AM

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