Using/retrieving the internal array of elements from a vector

V

Vince C.

Hi.

I'm writing a C++ application that uses GNU getopt_long() to parse command
line argument syntaxes.

http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html

I'd like to check multiple syntaxes hence multiple arrays of items, which
type are "option", defined in "getopt.h":

struct option {
const char *name;
int has_arg;
int *flag;
int val;
};

Since I want to make my program generic, I've used a vector<option> to
dynamically build an array of options, which I need to pass to function
getopt_long():

vector<option> options;

int getopt_long (int argc, char *const *argv, const char *shortopts, const
struct option *longopts, int *indexptr)

My program sucessfully back pushes option items into the vector but
getopt_long() doesn't seem to react as expected. Since I have no access to
the vector array data, I just pass &options[0] as the 4th argument,
longopts.

Can I be sure &options[0] is actually the address of the beginning of the
array? In other terms, does vector operator [] return the real adress of an
element in its container?

Thanks in advance.
 
K

Kai-Uwe Bux

Vince said:
Hi.

I'm writing a C++ application that uses GNU getopt_long() to parse command
line argument syntaxes.

http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html

I'd like to check multiple syntaxes hence multiple arrays of items, which
type are "option", defined in "getopt.h":

struct option {
const char *name;
int has_arg;
int *flag;
int val;
};

Since I want to make my program generic, I've used a vector<option> to
dynamically build an array of options, which I need to pass to function
getopt_long():

vector<option> options;

int getopt_long (int argc, char *const *argv, const char *shortopts, const
struct option *longopts, int *indexptr)

My program sucessfully back pushes option items into the vector but
getopt_long() doesn't seem to react as expected. Since I have no access to
the vector array data, I just pass &options[0] as the 4th argument,
longopts.

Can I be sure &options[0] is actually the address of the beginning of the
array?

Yes, std::vector is guaranteed to be contiguous (with the exception of
vector<bool>). Also, &options[0] will give you a pointer to the first
element.

In other terms, does vector operator [] return the real adress of
an element in its container?

Not quite, operator[] returns a reference or a const-reference. It does not
return an address.


Best

Kai-Uwe Bux
 
S

Sarath

Can I be sure &options[0] is actually the address of the beginning of the
array? In other terms, does vector operator [] return the real adress of an
element in its container?

Yes it is. operator[] returns the reference to the object. You can
take it's adress.
The vector allocation is contiguous so that you can easily access it
as array.

Before passing the elements to any function make sure that the vector
is not empty else it may produce some undesired result. also you have
to take care that, you should not overrite or use the reserved area of
vector.

dequeue is a container which has the close implementation of vector.
but the underlying memory is not contiguous. So you should not use
deque instead of vector.


Regards,
Sarath
http://sarathc.wordpress.com/
 
V

Vince C.

@Kai-Uwe Bux:
@Sarath:

Thanks for your lights. I've also tried with a static array of options, not
using a vector and I get the same behaviour: getopt_long() parses the array
of options only the first time (i.e. when parsing the first syntax, the
first array of options). If I iterate another array of options
getopt_long() immediately returns -1 and doesn't parse the array.

I'll post a new message on that particular topic.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top