Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a vector in plain C?

Tags:

c

vector

I was just compiling a C file and made a typo: swapped argc[0] for argv[0]. This, of course, gave me an error message from gcc:

SlidingWindow_file.c:443:29: error: subscripted value is neither array nor pointer nor vector

This error message makes perfect sense to me, except for one thing: What is a vector in the context of plain C (seeing as how this was a C file, not C++)? I can't seem to find any information about such a thing.

like image 613
Dominick Pastore Avatar asked Jun 30 '20 15:06

Dominick Pastore


People also ask

What is a vector in C?

A vector is a type of array you find in object-oriented languages like C++. Like arrays, they can store multiple data values. However, unlike arrays, they cannot store primitive data types. They only store object references – they point to the objects that contain the data instead of storing the objects themselves.

Can I use vector in C?

You can't. By definition, C knows nothing of any of the required components of a std::vector , including, but not limited to: C does not have namespaces, so it can't understand the std namespace. C does not have templates, so it can't understand the std::vector<T> type.

Why do we use vector in C?

Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user. This provides flexibility and reduces the time requirement with arrays to copy the previous elements to the newly created array.


1 Answers

vector in that error message refers to gcc vector extensions.

like image 129
Maxim Egorushkin Avatar answered Oct 19 '22 16:10

Maxim Egorushkin