Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to know the actual size of a passed in array parameter?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
A function: void funct(char* argv[])
I need to know how many array elements actually passed in for each call of this function, is there a easy way to do it?
Thanks!
 
blueruby

Unfortunately the answer is no, you are only being passed a pointer to the start of an array, from that pointer there is no way of determining the length of the array. You either have to know how to tell where the end of the array is by virtue of the data (ie have a NULL as the last element in the array as in the argv example you provide), by having the first element in the array define the size of the array or by passing a second variable containing the size of the array.

Whichever way you approach the problem the solution relies on the caller and callee knowing how the system works - there is no self-contained method of solving the problem.

Cheers
 
You can use this:

parse(vector<string> in);

int main(int argc, char *argv[])
{
//argc will be the number of elements passed in... you can //make parsing simpler like this:
vector<string> v;
for(int i = 0; i < argc; i++)
v.pushback((string)argv);
parse(v);
return 0;
}

parse(vector<string> in)
{
//Loop through and parse.
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top