Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Create you own local copy of the data and change the pointer in the list:</p> <pre><code>char **newentry = malloc(sizeof(char*) * 2); newentry[0] = strdup(myNewName); newentry[1] = strdup(myNewEmail); tableData[i] = newentry; </code></pre> <p>That's the easy part. The hard part is freeing the memory when you are finished. If this is just a little throwaway C program, you can probably get away without freeing memory since the operating system will automatically free it when the app exits.</p> <p>But if you want to do it Properly with a capital 'P', I'd make a couple of little functions to copy and free table items:</p> <pre><code>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); } </code></pre> <p>Now we can replace items in the list at will and we can easily free the memory by calling the tableFreeItem() function. Here is an example of how you can use these functions:</p> <pre><code>#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 &lt; 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 &lt; TABLE_SIZE; i++) tableFreeItem(i); </code></pre> <p><em>DISCLAIMER: I have not tried to compile or run this code, I just banged it off from the top of my head. It will probably work.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload