简体   繁体   中英

Length of char array (where “int” values are stored)

I need to store the some integer values in the char array with same size of memory . like int32 should fit in char[4] . so below is the thing I tried

int val = 123457;
char *data = val;

this stores properly, but If I allocate like this , I can not get the length or free it.

so I tried like this.

char *data = malloc(sizeof(val);

but How do I copy that int value .

Due to some requirement I have to store in the char array and get back int value from the char array .

Yes, you can do this, but it's not wise to mingle your data types like this unless you actually know what you're doing.

This is fraught with peril, as they say.

Still, if you wish to do it, and then copy the data, you can do it like this:

int val = 123457;
char *data = malloc(sizeof(val);
memcpy(data, &val, sizeof(val));

memcpy will solve your problem. You do something like this:

int val = 123457;
char data[sizeof int];
memcpy(data, &val, sizeof int);

Just look up man page for memcpy for more details. Basically it copies specified number of bytes from memory pointed to by second argument to that of first argument.

Note about endian-ness: If your machine is little-endian then data above will contain the bytes in reverse byte-order of what your would see if you represented the value 123457 in binary or hex. If you want to see bytes in data in big-endian then you may want use following code:

int val = htonl(123457);
char data[sizeof int];
memcpy(data, &val, sizeof int);

Here htonl converts whatever byte-order your machine is, into network order, ie big-endian.

with

int val = 123457;
char *data = val;

you probably meant

int val = 123457;
char *data = (char*)&val; // note the & to give the address of val

now data is pointing to the int, data is not holding the value, just pointing to it.

but if you want to copy the int value to the allocated memory:

char *data = malloc(sizeof(int));
int val = 123457;
memcpy( data, &val, sizeof(int));

now the int value is both stored in data and in val

you can also use a union

typedef union
{
    int myint;
    char mychars[sizeof(int)];

} u;

...

u myunion;
myunion.myint = 123456;

now you have the value 123456 in the int and you can get the characters by copying from myunion.mychars

Another way you can do this is by using union(one more useless thing of c++ if you ask me).

union utyp {
    int i;
    char ch[sizeof(int)];
};
union utyp x;
x.i=324255;

Union is making all of its variables share the same memory space

324255 is 100 11110010 10011111 to binary

so for example the (int)x.ch[2] is 4

You can do it easily using type casts eg

char data[8];
uint32_t a,b;  // ATTENTION! int can be 64 bit depending on architecture and compiler !
a=12345;
b=678910;
*(uint32_t *)data=a;
*(uint32_t *)(data+4)=b;
// to retrieve the values
uint32_t c=*(uint32_t *)(data+4);  // we'll get b

In C Char having the size of 1 word or byte means you can only store one character either a number..!!! While you trying to store a int32 number it is having 6 chars in terms of character. What are you saying it can't be possible with char in C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM