I was recently asked to represent a structure using a struct data type. As per my knowledge, a vector is represented as (10, 20, 30, 40 etc. ) It appears that it would be easier to access a vector by an indexed data structure, like an array. But I do not know how many members I would be needing in the struct data type. Then how will I define the structure ?
-
1you can try to create a dynamic array. allocate space for several elements, and when you fill it, create a new one, twice as big. you can check this for more info: http://stackoverflow.com/questions/2096571/how-is-c-stdvector-implemented – CIsForCookies Aug 25 '15 at 21:24
-
1If we are talking about vectors of a fixed dimension, say 2d, it is pretty natural to represent them as a structure of two fields `x` and `y`. – Eugene Sh. Aug 25 '15 at 21:33
-
I guess the OP means how many members he'll be needing, instead of dimensions? ...could be that I'm wrong, but then I suggest that the OP clarifies this? – Xabre Aug 25 '15 at 21:36
-
2`vector` is kinda ambiguous term when it comes to programming.. – Eugene Sh. Aug 25 '15 at 21:37
-
Hmm, valid point there, perhaps the OP can rephrase the question a little bit, so we're sure that we're talking about the same thing here? Or not... my spideysense senses ambiguity! – Xabre Aug 25 '15 at 21:45
-
When you say vector, are you talking about geometry? – Paul H. Aug 25 '15 at 22:08
3 Answers
A vector is basically a dynamic array, meaning that the underlying type is a pointer to an array, each time you add something, the array increases by one. In a very basically this would lead to a costly copy each time this was done; this is way they mostly try to either predict the next size ( by doubling the amount of available slots) or allowing one to resize to a best-guess, which, if missed, will still result in doubling. Anyway, an insert (thus not at the end of the array) will still require a costly copy. This means that a vector will be interesting for random access reads of data, while not so for writes.
When you look at the O-complexity, you'll see that this now makes sense.
- Random access - constant O(1)
- Insertion or removal of elements at the end - amortized constant O(1)
- Insertion or removal of elements linear in distance to the end of the vector O(n)
Knowing about the several data-containers is crucial to understand which one to pick under which circumstances. More often then not, I see programmers always using a vector (even the ones that are doing the job long enough to understand when to use which container).
I'm sure this explanation will give you enough to implement the real thing ;-)
sources:

- 1,292
- 1
- 11
- 17
-
What do you mean exactly? Everything described could perfectly be done in C :-) ...I know the link is to C++ reference, as there is an implementation done in C++ STL; but the same characteristics still apply :-) – Xabre Aug 25 '15 at 21:34
-
-
I agree that the question is ambiguous, the first time I read it, I believed, this is what he wanted... reading it again (and thanks to you), however, puts me in doubt :p I'm going to sit this one out and wait on clarification from the OP before I go ahead and delete this *answer* :-) – Xabre Aug 25 '15 at 21:47
In C99, you can define an FAM struct
.
struct vector {
size_t len;
int vals[];
};
Then you can dynamically allocate instances of the struct, depending on the length you need.
struct vector* v = malloc(sizeof(struct vector) + (n * sizeof(v->vals[0]));
v->len = n;
for (int i = 0; i < v->len; i++)
v->vals[i] = i;
A higher dimensional structure (e.g. matrix) is a bit trickier, but I'm not sure if you need that.

- 3,777
- 14
- 27
-
"Higher dimensions are a bit trickier, but I'm not sure if you need those." - this part makes no sense whatsoever. what you wrote already supports "higher" dimensions. – Karoly Horvath Aug 25 '15 at 21:52
-
Not really, it allocates the memory, but it doesn't include accessing the values which would require modifying the `struct`. For example, two dimensions or three dimensions. – Jason Aug 25 '15 at 21:56
-
What do you mean? The dimension of vector is it's length. You might get confused with matrices. – Eugene Sh. Aug 25 '15 at 22:01
-
vals[0] is x, vals[1] is y, ... if not, I have no idea what your intention was with that struct. – Karoly Horvath Aug 25 '15 at 22:02
-
That's what I was referring to, a higher dimensional structure. I changed the wording – Jason Aug 25 '15 at 22:11
If I wanted to represent an int vector in C, I would use:
- a malloc'd array
- a variable tracking allocated size
- a variable tracking used length
It gives:
struct int_vector {
int *arr;
unsigned int len;
unsigned int size;
};
Access to arr
is legal for 0<=i<len
.
I would add some utilities:
int resize(struct int_vector *vec, unsigned int new_len) {
if (new_len > vec->size) {
void *p;
unsigned int size = vec->size;
while(new_len > size) size *= 2;
*p = realloc(vec->arr, sizeof(int) * size);
if (p == NULL) return 0; /* cannot realloc */
vec->arr = p;
vec->size = size;
}
vec->len = new_len;
}
You can then easily implement push and pop:
unsigned int push(int_vector *vec, int val) {
if (resize(vec, vec->len + 1) == 0) return 0;
vec->arr[vec->len -1] = ival;
return vec->len;
}
int pop(int_vector *vec) {
vec->len -= 1;
return vec->arr[vec->len];
}
You could implement a truncate function to reduce the allocated size, but this is a simplistic but working example.
Unfortunately, as C has neither generics not templates, you would have to define a vector class for every and each type that you want to process...

- 143,923
- 11
- 122
- 252