Copy string to pointer to pointer

I am using this example:

char *myData[][2] =
{{"John", "j@usa.net"},
 {"Erik", "erik@usa.net"},
 {"Peter","peter@algonet.se"},
 {"Rikard","rikard@algonet.se"},
 {"Anders","anders@algonet.se"}};   

char **tableData[6];
tableData[0] = myData[0];
tableData[1] = myData[1];
tableData[2] = myData[2];
tableData[3] = myData[3];
tableData[4] = myData[4];
tableData[5] = NULL;//null terminated array

      

and want to place your own strings for name and email instead. (trying to put string xyz in myData then tableData) strcpy with myData doesn't work. I've tried all combinations of pointers and references, but it doesn't seem to copy the string. Any suggestions?

             ok--> strncpy(xyz, argv[i], strlen(argv[i]));
             ok--> strcpy(xyz + strlen(argv[i]), "\0");
run time stops here--> strncpy(myData[i][0], xyz, strlen(xyz));  
                   tableData[i] = myData[i];

      

+1


source to share


2 answers


The pointers in myData[][]

, as you initialized it, point to literal strings. This memory cannot be written.

You can allocate new memory for new lines and put pointers to new lines in myData

. Or for what you seem to be doing, just store the pointers to argv [] lines (until you plan on changing the lines later).



Also, make sure that the block of memory you are copying the lines to is large enough to hold the new line.

Monkey Edit software: enable \ 0 terminator; and make sure you free up memory when needed.

+3


source


Create your own local copy of the data and change the pointer in the list:

char **newentry = malloc(sizeof(char*) * 2);
newentry[0] = strdup(myNewName);
newentry[1] = strdup(myNewEmail);
tableData[i] = newentry;

      

This is the easy part. The hard part frees up memory when you're done. If it's just a little flashy C program, you can probably quit without freeing up memory, as the operating system will automatically free it when the application exits.

But if you want to do it right with capital "P" I would do some small functions to copy and free table elements:



void tableFreeItem(int i)
{
    if(tableData[i] != 0)
    {
       free(tableData[i][0]);
       free(tableData[i][1]);
       free(tableData[i]);
    }
}

void tableSetItem(int i, char *name, char *email)
{
    tableFreeItem(i);

    tableData[i] = malloc(sizeof(char *) * 2);
    tableData[i][0] = strdup(name);
    tableData[i][1] = strdup(email);
}

      

We can now swap the items in the list as desired, and we can easily free memory by calling the tableFreeItem () function. Here's an example of how you can use these functions:

#define TABLE_SIZE 5

char **tableData[TABLE_SIZE + 1]; /* +1 to make room for NULL terminator */

/* Clear out the table. This also sets up the NULL terminator at the end. */
    memset(tableData, 0, sizeof(tableData));

/* Copy the original data into the table */
    for(i = 0; i < TABLE_SIZE; i++)
        tableSetItem(i, mydata[i][0], myData[i][1]);

/* Change a data item */
    tableSetItem(3, "Adam Pierce", "adam@doctort.org");

/* Free memory when we have finished */
    for(i = 0; i < TABLE_SIZE; i++)
        tableFreeItem(i);

      

DISCLAIMER: I haven't tried to compile or run this code, I just hit it off my head. This will probably work.

0


source







All Articles