4

hey i would like to know how you could cast an Int array in C to an byte array and what would be the declaration method. I would appreciate if it is simpler and no use of pointers. thanks for the comments

ex: int addr[500] to byte[]

Plus I would also want the ending byte array to have the same array name.

5
  • An integer is 32 bits, where as a byte is 8 bits. Are you looking to have your byte array 4 times the size of the integer array, and store each integer over 4 bytes? Commented Feb 19, 2012 at 0:36
  • you want to access all bytes of each int, or only the low byte of each int? Commented Feb 19, 2012 at 0:36
  • Int is 4 or 8 bytes long (by definition at lest 2 I think)... How do you want it to convert to 1B type? To char[2000] or what? Do you know the difference between little and big endians? Commented Feb 19, 2012 at 0:36
  • What do you mean by "cast"? Do you want an array containing the same values in each position? Or do you want an array containing the same data but with a different interpretation? Commented Feb 19, 2012 at 0:37
  • as @joshhendo described above, I would like to store each integer in 4 byte. so I want to know how to do that. thanks Commented Feb 19, 2012 at 0:46

4 Answers 4

15

If you are trying to reinterpret the memory behind the int array as an array of bytes, and only then:

int ints[500];
char *bytes = (char *) ints;

You cannot do this without resorting to pointer casting, as declaring a [] array implies allocation on stack, and cannot be used for reinterpretation of existing memory.

Obviously, you need to know what you are doing. For each int there will be (typically, depending on platform etc.) 4 chars, so your new array would have 500*4 elements. Check out what the output of:

printf("char size: %d, int size: %d", sizeof(char), sizeof(int));

tells you to make sure.

If you are trying to interpret each int as a char, i.e. to get the same number of chars, as there were ints, then you cannot do this without a loop and manual conversion (to a new memory locaton, normally).

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

9 Comments

It should be made very clear though, this does not convert your ints to chars in any way, it just tricks the compiler to use the memory you allocated for ints for bytes instead.
It is worth noting that this is one of the very few pointer casts that is guaranteed to be safe by the C standard.
@DietrichEpp what does "safe" exactly mean?
@Vyktor I think it's safe because the alignment requirements of char are less strict than those of int
@Vyktor: The C standard guarantees that if you have some T *ptr, then you can cast to (unsigned char *) ptr and read the values byte by byte. The C standard says that the behavior is undefined for every other type besides unsigned char. For example, no strictly conforming program can execute *(unsigned short *) ptr, but a strictly conforming program may perform *(unsigned char *) ptr. It's also guaranteed that you can, e.g., copy an object to an unsigned char array and back with memcpy without corrupting it. See n1256 6.2.6.1.
|
4

You can use a union.

union {
  int ints[500];
  char bytes[0];
} addr;

2 Comments

This is a great answer--its actually the only answer that meets the OPs request of "no pointers." Of course, if they were trying to get around pointers because they don't understand them, a union might not be any better...
I think using union for type casting is UB.
1

If you want to

  1. reinterpret the memory behind the int array as bytes,
  2. want to avoid pointers syntactically --for whatever reason--, and
  3. making a copy of the int array is acceptable,

then you could create a new char array and copy the int array with memcpy:

int ints[500];
char bytes[500 * 4];
memcpy(bytes, ints, 500 * 4);

This is about the same as the first snippet from my original answer, with the difference that the bytes contain a copy of the ints (i.e. modifying one doesn't influence the other). Usual caveats about int size and avoiding magic constants/refactoring code apply.

Comments

0

It might be easier to use pointers but without pointers, try the following:

    #include <stdio.h>

    typedef unsigned char byte;

    void main() {
            int addr_size = 500;
            int addr[ addr_size ];
            byte bytes[ addr_size * 4 ];

            int i;
            for( i = 0; i < addr_size; i++ ) {
                    bytes[ i ] = (byte)( addr[ i ] >> 24);
                    bytes[ i + 1 ] = (byte)( addr[ i ] >> 16);
                    bytes[ i + 2 ] = (byte)( addr[ i ] >> 8);
                    bytes[ i + 3 ] = (byte)addr[ i ];
            }
    }

7 Comments

I'm afraid the shifts will raise more questions for the OP than answers they may give...
I'm afraid I don't have an answer to his constraints :) what would you suggest without pointers?
"Cannot do" ;-) At least not without writing unnecessarily complicated code and raising even more questions. Pointers are there for a damn-very-good reason in C, and writing C without them is like riding a bike without using legs and possibly one hand. C++ is a different story though.
What's unnecessary in my code? I don't think you can do better than that if you're making two copies of the same array. What you mentioned below just points to the same memory. And the shifting are still using pointers :D
I'm not criticizing your code -- moreover, assuming the no-pointers constraint, it's probably the solution. I'm referring to a more general situation in real world, where such a constraint leads to unnecessary code. I'm criticizing the constraint. No sane reviewer would prefer your code over mine, because no sane person would put that constraint in place. (Again, I'm not really comparing "your code" vs. "my code" here, that's not the point :-) )
|

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.