473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2353
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
7
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless,...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.