2

I am trying to store int value in char array, and access it again to retrieve value later. here is what i tried, but it is not working as i am looking for.

char mem[50];
int* size = (int*)&mem[0];

char* isFree = (char*)&mem[4];

char *t = (char *)&mem[4];

Now I want to store 1234, and 'f' in the char array according to the order. (within first 5 bytes)

I am not allowed to use other variables, have to use char array.

int a = 1234;
int *size = &a;

above one is not allowed.

10
  • You need to read the basics of pointers. To store a value you have to do *size = 1234; Commented Nov 12, 2015 at 16:08
  • i am not allowed to use other variables than using array space. so this won't work. I edited question. so it will be more clear. int a = 1234; size = (int *)&a; Commented Nov 12, 2015 at 16:11
  • 1
    @Haris That also after allocating memory or pointing to another variable. Commented Nov 12, 2015 at 16:12
  • actually i am implementing own malloc. so I am trying this for that work. Commented Nov 12, 2015 at 16:13
  • @ameyCU, size is pointing to the first element of mem[] array. Why would he need to allocate memory? Commented Nov 12, 2015 at 16:14

3 Answers 3

5

You have to use the type unsigned char to copy the memory or memcpy which does that for you. Aliasing a char array as an int is undefined in C.

store the value:

int value = 1234;
memcpy( mem, &value, sizeof(value) );

retrieve the value:

memcpy( &value, mem, sizeof(value) );

(The array mem must be large enough to hold the type int.)

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for answering my question. but this won't work. i am not allowed to use other variables than using array space. if not i can do this by using int a = 1234; size = (int *)&a; that's the issue
Why not char? I see both mentioned in the description of strict aliasing here. Is there some additional rule I am not aware of?
@anatolyg Signed char could potentially have a value with trap representation if using one's complement or sign+magnitude, if I read the C11 Standard correctly: 6.2.6.2,p2.
2

You can use sprintf -

int size=1234;
char mem[50];
sprintf(mem,"%d",size);

Note- This will append null terminator.

Comments

1

I have tried another way and i got a solution. I am posting it as a help for others.

char mem[50];
int* size = (int*)&mem[0];
*size = 1234;

char* isFree = (char*)&mem[4];
*isFree = 'f';

char *t = (char *)&mem[4];

printf("%d\n",*(int*)&mem[0]);
printf("%c\n",*(char*)&mem[4]);

I assigned a pointer to a pointer. I worked. Thank you all for answering.

1 Comment

On select systems, mem[] maybe not be properly aligned for an int array, so int* size = (int*)&mem[0]; is UB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.