Thread: Help converting an int to a char?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    Talking Help converting an int to a char?

    I just started using C, and I'm still learning, so I guess that makes me a noob.
    Anyway, what I've been trying to do is store and int variable in a char variable. After reading about the sprintf() function, I tried this:

    Code:
    int cur_num = 15;
    char buff[50];
    char code[50];
    
    static void make_code_string(void) {
    	sprintf(buff, "%d", cur_num);
    	code = code + buff;
    }
    But I keep getting

    error: invalid operands to binary +
    Help please?
    Last edited by motionman95; 03-08-2009 at 07:49 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you want code = code + buff to do? Do you want to add 15 to code? If so, then just do so:
    Code:
    code = code + 15;
    or
    code = code + cur_num;
    If you want to do something else, then you'll have to actually state what you want.

    (Note also that
    Code:
    code = code + 15;
    is the same as
    Code:
    code += 15;

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    I tried that, but I got:

    error: incompatible types in assignment

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah: code and code[50] are most definitely not the same thing. If you want to add to the end of a string, you use strcat. Note that code must be a legitimate (i.e., \0 terminated) string to begin with.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Would this be considered a legitimate string?

    Code:
    legitimate_string[50] = "Is this a legitimate string?\0"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Anything in "double quotes" is a legitimate string, even without the explicit \0.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    I don't get it. This code is perfect yet I keep getting the "warning: passing argument 2 of 'sprintf' makes integer from pointer without a cast" and "warning: passing argument 3 of 'sprintf' makes integer from pointer without a cast" errors. Whats wrong??!!

    Code:
    int cur_num = 1;
    char buff[50];
    
    sprintf(buff, "%d", cur_num);

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't have #include <stdio.h> at the top, you don't have sprintf.

    And are you sure that's the code you're dealing with? Argument 3, as you have it, is cur_num which is an integer, so you can't possibly be converting from a pointer.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    Thanks for the help! I've figured it out.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    One last question: how do you empty a char variable? This doesn't work:

    Code:
    char string[] = "";

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you mean by "empty a char variable"? Your example initialises a string named string to an empty string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    I'm meant this:

    Code:
    char string[50] = "This string is really filled dynamically filled.";
    And after I've filled "string" and am ready to use it again, I want to empty it, but this isn't working:

    Code:
    char string[] = "";

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, in that case you call just null terminate it at the start, i.e.,
    Code:
    string[0] = '\0';
    Actually, why not just use it immediately? You will be replacing the contents with another string, anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    I need to empty it first because I am using strcat to fill it. What should I do?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case my example should suffice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM