结构体数组初始化单列赋值
时间: 2023-11-24 14:47:50 浏览: 133
![PDF](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PDF.png)
结构体中数组成员赋值
![star](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
结构体数组的初始化可以通过以下方式进行单列赋值:
```python
from ctypes import *
# 定义结构体
class MyStruct(Structure):
_fields_ = [
("username", c_char * 10),
("age", c_int),
("sex", c_long)
]
# 初始化结构体数组
my_struct_array = (MyStruct * 3)()
# 单列赋值
my_struct_array[0].username = b"Tom"
my_struct_array[0].age = 20结构体数组的初始化可以通过以下方式进行单列赋值:
```python
from ctypes import *
# 定义结构体
class MyStruct(Structure):
_fields_ = [
("username", c_char * 10),
("age", c_int),
("sex", c_long)
]
# 初始化结构体数组
my_struct_array = (MyStruct * 3)()
# 单列赋值
my_struct_array[0].username = b"Tom"
my_struct_array[0].age = 20
my_struct_array[0].sex = 1
my_struct_array[1].username = b"Jerry"
my_struct_array[1].age = 22
my_struct_array[1].sex = 0
my_struct_array[2].username = b"Lucy"
my_struct_array[2].age = 25
my_struct_array[2].sex = 1
```
以上代码定义了一个名为`MyStruct`的结构体,包含了`username`、`age`和`sex`三个字段。接着,我们定义了一个长度为3的结构体数组`my_struct_array`,并通过单列赋值的方式对其进行初始化。在单列赋值时,我们可以通过数组下标的方式访问结构体数组中的每一个元素,并对其字段进行赋值。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)