DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Passing Arrays of Strings

  1. #1
    Join Date
    May 2013
    Posts
    2

    Passing Arrays of Strings

    I am trying to write a function that will return the list of family members given the family name.

    I want to pass the family name as an input (char array) and I want to return two arrays of strings that will return family member “roles” and their names. For example, if I pass the argument “McCoys” it will return something like:
    familyMemberRoles = {‘Dad’, ‘Mom’, ‘Son’, ‘Daughter’};
    familyMemberNames = {‘John’, ‘Jane’, ‘James’, ‘Jill’};

    Let’s say I will have a main function and a function ‘getFamilyMembers’. There is some data structure that the function getFamilyMembers will access to get the info for me. So something like this:
    Code:
    void getFamilyMemberNames (const char *familyName, char ** familyMemberRoles, char ** familyMemberNames)
    {
    	// Get the data here to populate familyMemberRoles, and familyMemberNames
    }
    
    int main ()
    {
    
    char *familyMemberRoles[];
    char *familyMemberNames[];
    
    getFamilyMembers(“McCoys”, familyMemberRoles, familyMemberNames )
    
    // Do something with the info received
    
    return 0;
    }
    Here are my questions:

    1. How do I initialize the arrays familyMemberRoles and familyMemberNames in the main function if I don’t know what their size will be? For example, some family can have more than 2 children so how would I initialize this and pass it to a function? And how do I “resize” the arrays in the function getFamilyMembers?
    2. Am I passing the variables familyMemberRoles, and familyMemberNames by reference correctly or do I do this differently?
    3. What is the best way of checking if the family is not found and the two arrays familyMemberRoles, and familyMemberNames are NULL?


    Sorry, these might be very fundamental questions but I am new to C++. I would appreciate any help!

    Alisha

  2. #2
    Join Date
    Jan 2021
    Posts
    8
    You can pass the values by reference or by value.

  3. #3
    Join Date
    Jan 2021
    Posts
    8
    For the size issue you can directly ask the user to input the size.

  4. #4
    Join Date
    Jan 2018
    Posts
    41
    As this is a C++ question, you wouldn't use pointers - you'd use std::vector<>. The issues around 'sizing' then go away.

    Code:
    #include <string>
    #include <vector>
    
    void getFamilyMemberNames (const std::string& familyName, std::vector<std::string>&  familyMemberRoles, std::vector<std::string>& familyMemberNames)
    {
    	// Get the data here to populate familyMemberRoles, and familyMemberNames
    }
    
    int main ()
    {
        std::vector<std::string> familyMemberRoles;
        std::vector<std::string> familyMemberNames;
    
        getFamilyMembers("McCoys", familyMemberRoles, familyMemberNames);
        // Do something with the info received
    }

Similar Threads

  1. Strings & Arrays
    By Gunsmith2112 in forum C++
    Replies: 5
    Last Post: 12-08-2008, 10:06 AM
  2. Replies: 3
    Last Post: 04-18-2008, 11:37 PM
  3. Marshal arrays of strings - how to?
    By marcellus in forum .NET
    Replies: 0
    Last Post: 11-23-2006, 08:08 AM
  4. Arg! OOP and strings/char arrays!
    By prototype in forum C++
    Replies: 3
    Last Post: 11-08-2001, 08:21 PM
  5. strings to arrays
    By kingNewbie in forum C++
    Replies: 2
    Last Post: 05-22-2001, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center