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.

  • 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? – joshhendo Feb 19 '12 at 00:36
  • you want to access all bytes of each int, or only the low byte of each int? – jcomeau_ictx Feb 19 '12 at 00: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? – Vyktor Feb 19 '12 at 00: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? – Dietrich Epp Feb 19 '12 at 00: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 – Apekshith Ramesha Feb 19 '12 at 00:46

4 Answers4

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).

Irfy
  • 9,323
  • 1
  • 45
  • 67
  • 1
    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. – Joachim Isaksson Feb 19 '12 at 00:38
  • 5
    It is worth noting that this is one of the very few pointer casts that is guaranteed to be safe by the C standard. – Dietrich Epp Feb 19 '12 at 00:42
  • @DietrichEpp what does "safe" exactly mean? – Vyktor Feb 19 '12 at 00:47
  • @Vyktor I think it's safe because the alignment requirements of `char` are less strict than those of `int` – Seth Carnegie Feb 19 '12 at 00:48
  • @JoachimIsaksson Edited to make that (hopefully) very clear, thanks. – Irfy Feb 19 '12 at 00:54
  • is the *bytes the array or just an element of the array? – Apekshith Ramesha Feb 19 '12 at 00:54
  • I strongly suggest to read introductory material to pointers and memory allocation in C, this cannot be answered simply, at the same time making it clear for you. `char *bytes` is the declaration; `*bytes` is the first element of the `bytes` array, same as `bytes[0]`; `bytes[i]` is the byte at index `i` (generally, "`i+1`th" element). – Irfy Feb 19 '12 at 00:57
  • Alignment is just a small part of the issue. The other part is aliasing. Only `char` types are allowed to alias other types in C. – R.. GitHub STOP HELPING ICE Feb 19 '12 at 06:33
  • 2
    @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. – Dietrich Epp Feb 19 '12 at 06:43
4

You can use a union.

union {
  int ints[500];
  char bytes[0];
} addr;
Simon
  • 2,067
  • 2
  • 17
  • 30
  • 1
    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... – setholopolus Feb 20 '18 at 16:10
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.

Irfy
  • 9,323
  • 1
  • 45
  • 67
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 ];
            }
    }
vimdude
  • 4,447
  • 1
  • 25
  • 23
  • 1
    I'm afraid the shifts will raise more questions for the OP than answers they may give... – Irfy Feb 19 '12 at 00:59
  • I'm afraid I don't have an answer to his constraints :) what would you suggest without pointers? – vimdude Feb 19 '12 at 01:01
  • 2
    "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. – Irfy Feb 19 '12 at 01:03
  • 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 – vimdude Feb 19 '12 at 01:10
  • 1
    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 :-) ) – Irfy Feb 19 '12 at 01:42
  • Actually... Look at my new answer xD – Irfy Feb 19 '12 at 01:49
  • where is the casting? most of those basic questions come from school homework. – vimdude Feb 19 '12 at 04:03