Solved

Pointer arrays...

Posted on 2004-10-12
2
205 Views
Last Modified: 2010-04-01
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
Comment
Question by:jewee
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
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

by:rstaveley
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now