Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store an integer in a char array

Tags:

arrays

c

I am trying to store an integer in a char array. How can I do that? This is my approach (by casting it the int to a char) but it does not work. What am I missing?

#include <stdio.h>

int main(int argc, char** argv) 
{
    char cArray[10] = {};

    // Store a character in the char array
    cArray[5] = 'c';
    printf("%c\n", cArray[5]);

    // Store an integer in the char array
    cArray[6] = (char) 0; // WHY DOES THIS NOT WORK???
    printf("%c\n", cArray[6]);
}
like image 843
user2426316 Avatar asked Nov 18 '13 15:11

user2426316


People also ask

Can I store integer in char array?

You can add the ASCII value of 0 to the value you want to store digit into the character array. For example if you want to store 0,1,...,9 into the character array A[10], you may use the following code. This works only if the integer you want to store is less than 10.

How do you add an integer to a char array?

Given an integer number N, the task is to convert it into a character array. Approach: The basic approach to do this, is to recursively find all the digits of N, and insert it into the required character array. Count total digits in the number. Declare a char array of size digits in the number.

How do you store values in a char array?

To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index number of the words and the column number will denote the particular character in that word.


1 Answers

Let's start with the basics.

For x86 architecture(assuming you use it) a char variable is stored in 1 byte and an int variable is stored in 4 bytes.

It is IMPOSSIBLE to store a random integer value in a char variable unless you have some compression schema and know that some integer values will not occur(no random!) in your program. No exceptions.

In your case, you want to store integers in a char array. There are four options:

1.If you want to store a random integer in this char array, then you should get the pointer to the index that you want to store the integer and cast it to an integer pointer and use it like that.

char mychars[10];
int * intlocation = (int*)(&mychar[5]);
*intlocation = 3632; // stores 3632

Beware that this will write to 4 bytes(4 char locations in your array) starting from the index you have specified. You should always check you are not going out of array.Also you should do the same casting for retrieving the value when needed.

2.If your values are between [0,255] or [-128,127] you can safely store your integers in a char since these ranges can be represented using a byte. Beware that char being signed or unsigned is implementation-dependent. Check this out!

mychars[5] = 54;

3.If your integer is just a digit, you can use the char representation of the digit.

mychars[5] = your_digit + 48; // 48 is the ascii code for '0'

4.If you want to store the string representation of your integer, then you should use itoa() and write each char of the resulting string to your array one by one. In that case, you should always check that you are not going out of array.

like image 108
Seçkin Savaşçı Avatar answered Sep 28 '22 02:09

Seçkin Savaşçı