首页 > 解决方案 > 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 编程新手,我有点迷失了。基本上我想创建一个没有内容的向量。

任何帮助,将不胜感激。问候

标签: cstructmalloc

解决方案


 v->LineContent[100]

是 a char,您尝试将其初始化为数组 / char *


如果你已经有一个v

memset(v, 0, sizeof(struct Vector));

将它归零(你必须#include <string.h>)。


写作

struct Vector new_vector = {0};

将其所有内容声明new_vector并初始化为\0.


推荐阅读