Changing bytes

suggest change

Once an object has an effective type, you should not attempt to modify it through a pointer of another type, unless that other type is a character type, char, signed char or unsigned char.

#include <inttypes.h>
#include <stdio.h>

int main(void) {
  uint32_t a = 57;
  // conversion from incompatible types needs a cast !
  unsigned char* ap = (unsigned char*)&a;
  for (size_t i = 0; i < sizeof a; ++i) {
    /* set each byte of a to 42 */
    ap[i] = 42;
  printf("a now has value %" PRIu32 "\n", a);

This is a valid program that prints

a now has value 707406378

This works because:

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents