Copying a 4 element character array into an integer in C

后端 未结 6 1775
一个人的身影
一个人的身影 2020-12-17 23:00

A char is 1 byte and an integer is 4 bytes. I want to copy byte-by-byte from a char[4] into an integer. I thought of different methods but I\'m getting different answers.

6条回答
  •  别那么骄傲
    2020-12-17 23:35

    unsigned int a = *(unsigned int*)str;
    

    This initialization is not correct and invokes undefined behavior. It violates C aliasing rules an potentially violates processor alignment.

提交回复
热议问题