Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointer arrays...

Posted on 2004-10-12
2
Medium Priority
?
257 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 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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

963 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