how to pass array of string to method

Posted on 2003-03-14
Medium Priority
Last Modified: 2010-04-01
how would i pass an array of strings to another method?

Question by:mb650cd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1

Expert Comment

ID: 8138179
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.

Accepted Solution

Rabo earned 200 total points
ID: 8138291
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"
LVL 11

Expert Comment

ID: 8141988
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

by:Mayank S
ID: 8149839
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!


Expert Comment

ID: 9492023
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.


EE Cleanup Volunteer
LVL 30

Expert Comment

by:Mayank S
ID: 9493426
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.

Expert Comment

ID: 9494731
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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question