簡體   English   中英

c編程,創建存儲指針,struc的動態數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM