[英]c programming, create dynamic array which stores pointers, 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]
是一个char
,您试图对其进行初始化,好像它是一个数组/ char *
。
如果您已经有一个v
,
memset(v, 0, sizeof(struct Vector));
将其归零(您必须#include <string.h>
)。
写作
struct Vector new_vector = {0};
声明new_vector
并将其所有内容初始化为\\0
。
您可以使用{}
来指定数组中各个元素的值(在本例中为charts) or, as this is a special case, use
for the string (a null-terminated sequence of
charts) or, as this is a special case, use
“” for the string (a null-terminated sequence of
图表for the string (a null-terminated sequence of
)进行初始化。 您写的是两者的结合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.