Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Pointer arrays...

Posted on 2004-10-12
Medium Priority
252 Views
I have 7 pointer arrays which for a particular method, I will have to condense into one....

char *configFilegr = {"GR_1"..}
char *configFilegr2 = ....

How would I do that???
0
Question by:jewee
[X]
###### 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

LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 12289949
You could use a function like

char** merge_arrays( char** p1, unsigned int n1, char** p2, unsigned int n2) {

char** p = new char*[n1 + n2];

memcpy(p, p1, n1);
memcpy(p + n1, p2, n2);
}

But, I'd *strongly* advise to use std::vecors or any other STL container for that, since they're way easier to handle
0

LVL 17

Expert Comment

ID: 12291015
Here's a std::vector approach...

--------8<--------
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

template <typename T>
vector<T> merge_arrays(T* arr1,size_t arr1_sz,T* arr2,size_t arr2_sz)
{
vector<T> retval;
retval.reserve(arr1_sz+arr2_sz);
retval.insert(retval.end(),arr1,arr1+arr1_sz);
retval.insert(retval.end(),arr2,arr2+arr2_sz);
return retval;
}

int main()
{
const char *arr1[] = {"one","two"};
const char *arr2[] = {"three","four","five"};
vector<const char*> v = merge_arrays(arr1,2,arr2,3);
cout << "Counting: ";
copy(v.begin(),v.end(),ostream_iterator<const char*>(cout," "));
cout << '\n';
}
--------8<--------
0

## Featured Post

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base classâ€¦
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
###### Suggested Courses
Course of the Month11 days, 13 hours left to enroll