c - c编程,创建存储指针的动态数组,struc
问题描述
所以我的问题有点烦人。我必须创建一个名为 vector 的结构,它包含一个字符串 ~ 字符数组。供以后使用。到目前为止我写的:
vector.h
// forward declare structs and bring them from the tag namespace to the ordi$
typedef struct Vector Vector;
// actually define the structs
struct Vector {
int SortPlace;
char LineContent[100];
};
vector.c
// Initialize a vector to be empty.
// Pre: v != NULL
void Vector_ctor(Vector *v) {
assert(v); // In case of no vector v
(*v).SortPlace = 0;
(*v).LineContent[100] = {""};
}
我的错误信息是:
vector.c: In function ‘Vector_ctor’:
vector.c:13:24: error: expected expression before ‘{’ token
v->LineContent[100] = {""};
由于我是 C 编程新手,我有点迷失了。基本上我想创建一个没有内容的向量。
任何帮助,将不胜感激。问候
解决方案
v->LineContent[100]
是 a char
,您尝试将其初始化为数组 / char *
。
如果你已经有一个v
,
memset(v, 0, sizeof(struct Vector));
将它归零(你必须#include <string.h>
)。
写作
struct Vector new_vector = {0};
将其所有内容声明new_vector
并初始化为\0
.
推荐阅读
- node.js - 通过基于嵌套和子嵌套文档的过滤器选择一个字段,例如使用 mongo 的 Post->Comment->Likes
- bash - 组合多个 bash 脚本
- php - 根据列值获取正确的行
- .net-core - EF Core 3.0 附加文件映射和更新
- javascript - 如何在一周内获得第二天
- r - `format` 对 UTF-8 字符串有什么作用?
- php - Yii2 无法将本地时间转换为 GMT 时区
- postgresql - 创建的 Postgresql 视图在 Npgsql 中不起作用
- django - 安装 webpack_loader (webpack-template-loader) 在 assignment_tag 上出现属性错误
- php - 我想根据动态下拉列表中的 id 选择显示名称