简体   繁体   中英

c programming, create dynamic array which stores pointers, struc

So my problem is kinda annoying. I have to create a struc called vector, which holds a string ~ array of chars. For later use. What Ive written so far:

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] = {""};
}

My error message is:

vector.c: In function ‘Vector_ctor’:
vector.c:13:24: error: expected expression before ‘{’ token
  v->LineContent[100] = {""};

Since im new to c programming im kinda lost. Basically i want to create a vector with no content.

Any help would be appreciated. Greetings

 v->LineContent[100]

is a char , which you are trying to initialize as though it were an array / char * .


If you already have a v ,

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

will zero it (you'll have to #include <string.h> ).


Writing

struct Vector new_vector = {0};

declares new_vector and initializes all its contents to \\0 .

You can use {} to specify the value of individual elements of the array (in this case, charts) or, as this is a special case, use "" for the string (a null-terminated sequence of charts) to initialize it with. What you wrote was a combination of the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM