简体   繁体   中英

How to copy data values in char** into a new char**?

What I am working on:
Hello. I currently have a char** variable, which is a pointer to an array of strings. I have a loop that occurs, and in this loop the char** needs to be backed up to a vector of structs. So the struct has a variable type of char** inside of it.
Unfortunately, for this piece I have to use the char** type. I am unable to use the char* vName[] type.

What my problem is:
The problem I am currently facing is that when I add a new struct, the char** points to the newest data available in ALL of the structs, not the most recent one.

What I have tried:
I have tried using strcpy , memcpy , and using the plain old values, but they don't seem to work. I have tried using newItem.storedVars[i] = newVars but that doesn't seem to work either.

How can I get or copy the data in the new char** array and store it into the struct without it being modified again by the loop?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct StringHolder{
    char** storedVars;                              //stored char** variable
};

int main(int argc, char *argv[]){
    char** newVars;                                 //stores current char** values
    int counter = 100;                              //counter
    vector<StringHolder> vectorOfStructs;

    while(counter!=0){                              //while counter is going downward
        StringHolder newItem;                       //create a new string
        char** storedVarsToAdd;                     //stored variables that I will be adding

        newVars = externalVarsFetcher();            //get the new char** variable from the external function

        storedVarsToAdd = newVars;                  //this statement doesn't work

        //neither does this statement
        for(int i = 0; i < 10; i++){
            storedVarsToAdd[i] = newVars[i];
        }
        newItem.storedVars = storedVarsToAdd;       //take the new item I created, update it's char** value with a new one
        vectorOfStructs.push_back(newItem);         //push the struct onto the vector
        counter--;                                  //continue through the array
    }
}

Here's how to store the command-line arguments, which is the same thing you're trying to do:

std::vector<std::string> args;
for (int i=0; i<argc; i++)
   args.push_back(argv[i]);

Your problem is that you are just juggling pointers, you are not copying the strings. You should be using std::string , but this sounds like homework so you have probably been told not to do that. If that's not the case use std::string

If you must use char** etc.:

for(int i = 0; i < 10; i++){
    storedVarsToAdd[i] = strdup(newVars[i]);
}

strdup will allocate memory and copy the string for you.

Now you have a potential memory leak but that's a different issue (hint - use std::string ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM