Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (int a=10) into a (char string[200]).
2 Answers
Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (
int a=10
) into a (char string[200]
).
Yes, there is use the string version of printf
snprinf(string,200,"%d",a);
It is also possible to generate the string with a loop with div and mod by 10 if you want to do above by hand. See another answer for an example of this https://stackoverflow.com/a/49615617/391691
To get your int back from the string sscanf
, atoi
or atol
can be used
#include <stdio.h>
#include <stdlib.h>
void main(){
int a;
char string[200];
a = 10;
snprintf(string,200,"%d",a);
printf("As string:%s As int:%d\n",string,atoi(string));
}
The interpretation of this question is still not clear to me, this represents an alternative interpretation of the question compared to previous answers https://stackoverflow.com/a/58584291/391691

- 3,373
- 2
- 24
- 38
-
That's what I was looking for. Could it be implemented with memcpy also? – Stelios Oct 28 '19 at 00:40
-
`memcpy` would represent the other interpretation of this question where you just store the raw data in the char array -- which will at this point not be a string. – Simson Oct 28 '19 at 00:42
-
@Stelios if this is the right answer for you, please consider rephrasing you question to match the answer for future readers. I will then delete my answer below. – Ente Oct 28 '19 at 08:10
Is there any way I can put an integer into a string of known size and if yes how can I read it? For example an (int a=10) into a (char string[200]).
In this answer I work with the minimal question stated, which does not give details about any of the following:
- Representation / encoding of the integer in
string
- Architecture this should run on
- The position where to insert the integer
If this question is about converting an integer to its ASCII representation the answer by @Simson is what you are looking for.
C allows you to "put"[1] an integer into any position of a c style string by casting the char
array to an int*
.
// string: the array (char*)
// pos: position in the array (size_t)
// to_insert: the value to insert (int)
(*(int*)(&string[pos])) = to_insert;
But be warned: This is a sledgehammer approach. You have to be very careful when doing this or your code might show undefined behavior on different platforms. For example you have to manually check and answer:
- Memory alignment: will I try to insert the integer at a position which is not addressable by the architecture?
- Endianness: is the way that I write this integer to the array actually what I want?
sizeof(int)
: does the integer still fit into the array? Maybe consider using any fixed length integer types such asuint16_t
,uint32_t
, ...- and possibly a few more
The following example shows the usage of the method discussed:
#include <stdio.h>
void insert_at_pos(char* arr, size_t pos, int to_insert){
(*(int*)(&arr[pos])) = to_insert;
}
int main(void) {
char string[200] = {0};
insert_at_pos(string, 3, 10);
//print char by char as hex
for (size_t i=0; i < sizeof(string)/sizeof(*string); ++i){
printf("%x ",(unsigned char)string[i]);
}
}
Running the example:
$ ./main
0 0 0 a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[1]: And by "put" I understand write as-is.

- 2,301
- 1
- 16
- 34
-
1In this example you make assumptions about the size of int to be 32 bit. – Simson Oct 28 '19 at 00:11
-
*You can do this by casting your `char` array string to an `int*`.* No, you can not safely do that. `(*(int*)(&string[pos])) = to_insert;` violates the [strict aliasing rule](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule] and may also invoke undefined behavior per [**6.3.2.3 Pointers**, paragraph 7](https://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p7): "A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, **the behavior is undefined**." – Andrew Henle Oct 28 '19 at 00:24
-
(cont) Just Google [`SIGBUS` ARM](https://www.google.com/search?q=sigbus+arm) – Andrew Henle Oct 28 '19 at 00:28
-
I just answered what the question was asking for: "Is there any way I can put an integer into a string of known size and if yes how can I read it?" Neither does the author say a single word about converting the integer to ASCII, nor does he state anything about `sizeof(integer)` or the position where to insert that int. I do know that my answer might cause undefined behavior on different systems but it tries to do exactly what the author is asking for. – Ente Oct 28 '19 at 08:07
-
1Write a comment about the interpenetration of the question and address the issues pointed out then this is good reference for someone in the future who have another interpretation of the question. – Simson Oct 28 '19 at 08:49