一、问题引入
定义如下数据结构
使用scanf输入ip(字符串)转4字节数字。
将会导致ServerPort前面3字节清零。
| | | | ServerPort[0] | ServerPort[1] | ServerPort[2] | ServerPort[3] |
---|
ServerIP[0] | | | | | | | |
| ServerIP[1] | | | | | | |
| | ServerIP[2] | | | | | |
| | | ServerIP[3] | | | | |
原因:对scanf使用%d输入时,会认为后面给的参数为int型,将会按照int型对其地址赋值。
上面例子实际给的是char型变量,那么每个ServerIP将会按照ServerIP及其后面3个字节组成的int赋值