putting int in my char array

This is a discussion on putting int in my char array within the C Programming forums, part of the General Programming Boards category; Hi, I have a char array and i want to put an int inside it. I tried some thing like ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    putting int in my char array

    Hi,

    I have a char array and i want to put an int inside it. I tried some thing like this but it didnt work

    int num = 2;

    char *myarray = malloc(sizeof(int)*5);

    myarray[0] = (char)num;

    can i used sprintf?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    What are you trying to do?

    Take 5 and put '5' inside for example?

  3. #3
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    ""Take 5 and put '5' inside for example?""

    exactly

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Is there any limit to your number? If it's a one digit number you can do this:

    Code:
    myarray[0] = num - '0';
    myarray[1] = '\0';
    If not, then you have to use sprintf() or snprintf(). snprintf() is safer because it won't overrun your buffer:

    Code:
    char szBuffer[BUFSIZ];
    int num = 100;
    
    snprintf(szBuffer,sizeof(szBuffer),"%d",num);
    If you malloc() your buffer, though, you can't use sizeof() to determins the size.

  5. #5
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    ok..

    after this

    char szBuffer[BUFSIZ];
    int num = 100;

    snprintf(szBuffer,sizeof(szBuffer),"%d",num);


    i can simply do

    myarray[0] = szBuffer;

    ?

    also im getting this warning implicit declaration of function `snprintf'

    this is my code

    Code:
    char *encodedRow = malloc(sizeof(int)*colnum);
    char charRun[100];
    run=2; /* can be two digits */
    
    snprintf(charRun,sizeof(charRun),"%d",run);
    encodedRow[encodedCounter++] = charRun;
    Last edited by waxydock; 06-01-2007 at 12:28 AM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Include <stdio.h> if you didn't already. That's where snprintf() should be located.

    i can simply do

    myarray[0] = szBuffer;

    ?
    No, that's wrong because myarray[0] is a char and szBuffer is a char array (or even a char * in this context).

    char *encodedRow = malloc(sizeof(int)*colnum);
    Why oh why would you allocate memory to hold ints in a char array? A char array doesn't hold ints. It holds chars.

    In your case, you have to realize that down deep inside the computer everything is just numbers. What is the difference with all the different data types? To the computer hardware, not a heck of a whole lot. Just mainly their sizes is all that matters. On a 32-bit machine, a char is 1 byte and an int is 4 bytes. This means you're allocating about 4 times too much memory.

    You need to get over the fact that you have an int that you want to print. It's just a number. The way to print it though is to convert each digit of it into its ASCII value because you want it to be printed in text form. All you're doing is transferring one number to another number.

    Let's say you want to print 97. '9' is 57. '7' is 55. This means if you want 97 to be inside your char array, you just want '9' and '7'. '9' and '7' are just individual bytes, 57, and 55. 97, though, if it's inside of an int, is 4 bytes in length (although it's a small enough number to fit inside a byte). This conversion is all that sprintf() and snprintf() do for you in this regard. They just convert data from one form of numbers to another.

    When you convert from an int to a char array, you're just simply transfering data from one form to another. To print 97, you just need to print '9' and '7'. That's it.

    char *encodedRow = malloc(sizeof(int)*colnum);
    char charRun[100];
    run=2; /* can be two digits */

    snprintf(charRun,sizeof(charRun),"%d",run);
    encodedRow[encodedCounter++] = charRun;
    Same point about malloc().

    You're using snprintf() wrong. You're allowing it to print up to 100 digits of the number into charRun, although it'll only take one since you're telling it to put run into it, which is simply 2.

    I don't know what you're trying to do with encodedRow.

    Here's an example of how to use snprintf(). I did this recently while working on a Windows program since I needed to see the output of some numbers. The message box isn't able to take numbers. It only takes a char array. This means to print numbers you have to convert:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
    	int x;
    	char szBuffer[BUFSIZ];
    	
    	printf("Enter a number: ");
    	fflush(stdout);
    	scanf("%d",&x);
    	printf("You entered number %d\n",x);
    	
    	snprintf(szBuffer,sizeof(szBuffer),"Your number is %d...",x);
    	MessageBox(NULL,szBuffer,"snprintf() demo...",MB_OK);
    	
    	return 0;
    }

  7. #7
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    ok, i understand what you are saying now, but i have a problem.

    Currently i am adding char's to my char* array like normal and am keeping a counter to the current element im at. So for example:

    Code:
    char *myarray = malloc(sizeof(char)*1000);
    int counter =0;
    
    myarray[counter++] = 'a';
    myarray[counter++] = 'b';
    myarray[counter++] = 'c';
    where counter is holding the current position i can add a char to.

    But if i add the int via scanf to this char array (like i have below), how do i increment the counter. Do i need to calculate the number of digits in my int and increment by that much?

    Code:
    int run=21;
    snprintf(myarray,sizeof(char)*1000,"&#37;d",run);
    also by doing the above code, will this append to my char array or override it?
    Last edited by waxydock; 06-01-2007 at 01:43 AM.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Call strlen() on the string (as long as it has a '\0' somewhere inside of it!). What you get will be the index you can start writing at (assuming you still have room left in the string).

    So for my example:

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
    	int x;
    	size_t stLength;
    	char szBuffer[BUFSIZ];
    	
    	printf("Enter a number: ");
    	fflush(stdout);
    	scanf("&#37;d",&x);
    	printf("You entered number %d\n",x);
    	
    	snprintf(szBuffer,sizeof(szBuffer),"Your number is %d...",x);
    	MessageBox(NULL,szBuffer,"snprintf() demo...",MB_OK);
    	
    	stLength = strlen(szBuffer);
    	snprintf(&szBuffer[stLength],sizeof(szBuffer) - stLength, "\n%d * 2 = %d\n",x,2*x);
    	MessageBox(NULL,szBuffer,"snprintf() demo...",MB_OK);
    	
    	return 0;
    }
    Edit:

    Your code here:

    Code:
    int run=21;
    snprintf(myarray,sizeof(char)*1000,"%d",run);
    When this code is called this is what the string myarray looks like:

    Code:
    '2', '1', '\0', <unknown data>
    Last edited by MacGyver; 06-01-2007 at 01:56 AM.

  9. #9
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    I see, so what ever i added to the array before the int will be overwritten??
    ie. if i did myarray[0]='a'; before the snprintf.. it would no longer be in the array? if so.. how can i avoid this.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I just showed you how to do it. Find out where the last element of the string is, and write from that position.

    I think you're having a misconception of data being "added" to an array. It's not added, in the sense of being tacked on to the end. snprintf() will write from the address you give it. Your char array is layed out contiguously in memory. This means you just tell it where to write from.

    If your array look like this:

    Code:
    'a', 'b', 'c', 'd', 'e', '\0', <unknown data>
    And you want to write it after the 'e', then you find out the length of the string with strlen(). If you don't have the '\0', but you're using a counter variable, then use your counter variable instead of the following:

    Code:
    size_t stLength;
    
    ....
    
    stLength = strlen(yourstring);
    stLength now contains the length of the string. Realize that snprintf() doesn't care if you pass it a char * that points at the beginning of the array or in the middle. It'll just start writing from the location you give it onward.

    The pointer you should give it is this:

    Code:
    &yourstring[stLength]

  11. #11
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Quote Originally Posted by waxydock View Post
    ok, i understand what you are saying now, but i have a problem.

    Currently i am adding char's to my char* array like normal and am keeping a counter to the current element im at. So for example:

    Code:
    char *myarray = malloc(sizeof(char)*1000);
    int counter =0;
    
    myarray[counter++] = 'a';
    myarray[counter++] = 'b';
    myarray[counter++] = 'c';
    where counter is holding the current position i can add a char to.

    But if i add the int via scanf to this char array (like i have below), how do i increment the counter. Do i need to calculate the number of digits in my int and increment by that much?

    Code:
    int run=21;
    snprintf(myarray,sizeof(char)*1000,"%d",run);
    also by doing the above code, will this append to my char array or override it?

    You can start writing at your counter, by adding your counter to the array like this:

    snprintf(myarray+counter,sizeof(char)*1000,"%d",run);

    All printf functions return a value. This is the number of characters that were printed. You can add them to your counter afterwards.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    In order to make sure you don't have an issue with overflowing the buffer, if you add an offset to the string to print, you must subtract the same offset from the size that you are telling snprintf() it is allowed to write.

  13. #13
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    Thanks MacGyver, nvoigt this solution works great!.. thanks

    counter = counter + snprintf(myarray+counter,sizeof(char)*1000,"%d",ru n);

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    *sigh*

    Code:
    counter = counter + snprintf(myarray+counter,(sizeof(char)*1000)-counter,"%d",run);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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