Store integer in char array

I am trying to store an integer in a char array. How can i do this? This is my approach (discarding int on 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]); } 
+6
source share
7 answers
 cArray[6] = (char) 0; // WHY DOES THIS NOT WORK??? printf("%c\n", cArray[6]); 

This code is trying to print a character encoded with 0 ; assuming ASCII, nothing will be displayed because there is no printable font for this code.

If you intend to save the ASCII code for the character '0' and print it, you need to write

 cArray[6] = 48; // same as writing cArray[6] = '0' (ASCII) printf( "%c\n", cArray[6] ); 

This will print 0 on the console.

If, on the other hand, you want to save any arbitrary integer value 1 to cArray[6] and display that value, then you need to use the conversion specifier %d :

 cArray[6]; printf( "%d\n", cArray[6] ); 


1. That is, any integer that fits into the char range, anyway
+7
source

Let's start with the basics.

For the x86 architecture (if you use it), the char variable is stored in 1 byte, and the int variable is stored in 4 bytes.

It is IMPOSSIBLE to store the integer value random in a char variable if you do not have any compression scheme, and you know that some integer values ​​will not occur (without random!) In your program. With 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 a pointer to the index that you want to store the integer and pass it to the integer pointer and use it that way.

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

Remember that this will be written in 4 bytes (4 char locations in your array), starting at the index you specified. You should always check that you are not leaving the array. You must also do the same as for getting the value when necessary.

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

 mychars[5] = 54; 

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

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

4.If you want to keep the string representation of your integer, then you should use itoa() and write each char from the resulting string to your array one at a time. In this case, you should always check that you are not leaving the array.

+10
source

Typically, a computer does exactly what you tell it. The DWIM instruction (doing what I mean) has not yet been invented.

 cArray[6] = (char) 0; // WHY DOES THIS NOT WORK??? 

sets index position 6 but

 printf("%c\n", cArray[5]); 

displays index position 5.

+1
source

Replace

 cArray[6] = (char) 0; // WHY DOES THIS NOT WORK??? printf("%c\n", cArray[5]); 

By

 cArray[6] = (char)51; printf("%c\n", cArray[6]); 

Must display "3"

I think you will understand your mistake ... 0, since int does not represent the printable character '0', but the completion line is NULL

51, since int represents the character '3'

0
source

You can use itoa(0,&cArry[6],10) .

0
source

Use the desired specifier format.

cArray[6] = (char) 0; stores the integer 0 in the array element cArray[6] . Its printf() , which tricks the OP into thinking that this did not work.

Using %c , says OP wants a character that is encoded with 0. (This is usually not visible). Use% d to print the integer value of cArray[6] .

 printf("%d\n", cArray[6]); 
0
source

You can add an ASCII value of 0 to the value you want to store in an array of characters. For example, if you want to store 0,1, ..., 9 into an array of characters A [10], you can use the following code.

  for(j = 0; j<k; j++) { A[j] = j + '0'; // Same effect if you use A[j] = j + 0x30 } 

This only works if the integer you want to store is less than 10. Otherwise, you will have to extract the numbers using the modulo operator and store.

0
source

Source: https://habr.com/ru/post/958320/


All Articles