C++ to C


I have some code in C++ that uses an std::vector of std::strings to store an array of strings. I need to convert it into C. I thought that I would just create c versions of the functions I need (vector.back,vector.push_back,string.operator+=) but am getting lost in the double pointers. Can anybody lead me on the right track here?

Featured Replies in this Discussion


std::vector and std::string do a lot of work internally. You'd probably be better off converting the 'what' instead of the 'how'. Ask yourself what the code is trying to accomplish and write C code to do it.


I was thinking of rather than storing a char** to store a single char* string with some delimiting character. I really need a dynamic array of strings so I do not see any other choice.


You're focusing too much on the 'how'. Explain what the program does and someone can help make suggestions.


Consider why you must convert to C. I cont see many reasons where C would be necessary over C++.


It's not all that difficult to do. Here's an example of push_back

const int BlockSize = 10; // allocate this number of elements at one time
int MaxArraySize = 0; // initial size of the array
int CurrentSize = 0; // current number of elements used in the array

char** array = 0; 

void ReallocArray()
   MaxArraySize += BlockSize;
   array = realloc(array,MaxArraySize);

char* push_back(const char* item)
    if( (CurrentSize+1) >= MaxArraySize )
    array[CurrentSize] = malloc(strlen(item)+1);
    strcpy(array[CurrentSie], item);
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
View similar articles that have also been tagged: