0

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

5
  • 1
    (*v).foo ~> v->foo ((*v).foo is not wrong, but it's awkward to read) Commented Dec 4, 2018 at 20:10
  • 1
    In vector.c: #include "vector.h" Commented Dec 4, 2018 at 20:11
  • 1
    in vector.h enclose your code in #ifndef VECTOR_H_INCLUDED newline #define VECTOR_H_INCLUDED and #endif. See Why include guards? Commented Dec 4, 2018 at 20:12
  • Hi Swordfish, I have included vector.h and did enclose my code.. didnt show it in code, since im quite sure its not the error Commented Dec 4, 2018 at 20:30
  • (*v).LineContent[100] = {""}; ===> (*v).LineContent[0] = 0; Commented Dec 4, 2018 at 20:33

2 Answers 2

2
 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.

Sign up to request clarification or add additional context in comments.

Comments

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 ofcharts) to initialize it with. What you wrote was a combination of the two.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.