• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

how to pass array of string to method

how would i pass an array of strings to another method?

mb
0
mb650cd
Asked:
mb650cd
  • 2
  • 2
  • 2
  • +1
1 Solution
 
RaboCommented:
By reference...
using a pointer...
using a wrapper-class providing a proper copy-constructor...
using a proper container-class.

Basically it depends on what you plan to do with/on the
data after the passing and how much heap, stack, complexity
and cpu-cycles you want to dedicate to the passing-process.
0
 
RaboCommented:
For example:

std::string theArray[];

void passArray( std::string anArray[] )
{
// .... do some modification to anArray;
}

passArray( theArray );

This will pass a pointer to the array to
the method, therefore any modification you
do to "anArray" will in fact modify "theArray"
0
 
cupCommented:
Since you are using strings, why not use vectors as well.  Then you won't have to worry about array sizes.  All you have to worry about is how to analyze the core dump if you ever get one.

typedef std::vector<std::string> StrVec;

void passiton (StrVec& ropes)
{
   // do something with ropes.
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mayank SAssociate Director - Product EngineeringCommented:
In case you are not using the string class but implementing your string as a character array, then:

void func ( char str[][80], int range, .... /* other parameters if reqd */ )
{
  // refer to one string as str[i], 0 <= i < range

} // end of func ()

Suppose you have a 2-D array of characters in the main () function:

char a[10][80] = { "First", "Second", .... /* etc */ } ;

// call func () as:

func ( a, 10 ) ; // or whatever the range should be

Hope that helps!

Mayank.
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Rabo

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Rabo's method will not work if the questioner is not working with the string class but if his string is like a character-array. Since that is not specified in the question, split between Rabo and mayankeagle.
0
 
tinchosCommented:
You're right mayankeagle

But in my opinion, Rabo's comment was enough to be considered as an answer, as I considered that when the asker talks about an array of strings he would be talking about string[].

Nevertheless, it's just my interpretation, and unfortunately the asker didn't close the question himself to know exactly if rabo's comment was enough or not.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now