468,738 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Using/retrieving the internal array of elements from a vector

Hi.

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

http://www.gnu.org/software/libc/man...g-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<optionto
dynamically build an array of options, which I need to pass to function
getopt_long():

vector<optionoptions;

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.

--

Vince C.
Jul 16 '07 #1
3 2188
Vince C. wrote:
Hi.

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

http://www.gnu.org/software/libc/man...g-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<optionto
dynamically build an array of options, which I need to pass to function
getopt_long():

vector<optionoptions;

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
Jul 16 '07 #2
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/

Jul 16 '07 #3
@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.

--

Vince C.
Jul 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
reply views Thread by Phil | last post: by
1 post views Thread by TF | last post: by
19 posts views Thread by Alf P. Steinbach | last post: by
1 post views Thread by Brian McGuinness | last post: by
49 posts views Thread by vfunc | last post: by
7 posts views Thread by hlg | last post: by
19 posts views Thread by arnuld | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.