Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing Arrays of Strings in C++

    Hi,

    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
    New Coder
    Join Date
    Jul 2013
    Posts
    48
    Thanks
    0
    Thanked 2 Times in 2 Posts
    initialize before main function and for varying children problem you can use get array from user code
    for 2nd q.. you are doing fine
    2rd you can use code to print the arrays in this way you can find either no of member of array or if the array is null


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •