My C is a little more than rusty at the moment, so I'm failing to create something I think should be pretty basic.
Allow me to refer to character arrays as strings for this post. It will make things clearer for both me and you.
What I have is an array that can hold 1 or more strings. For instance {"ab", "cd", "ef"}. I want to make another array to store multiple versions of the array of strings. So something like {{"ab", "cd", "ef"}, {"gh", "ij"}, {"kl"}}.
What I have right now is:
char *arrayOfStrings[50]; // a single array to hold multiple strings
char **arrayOfArraysOfStrings[10]; // array to hold multiple snapshots of arrayOfStrings
The array of strings changes over time and I'm using the array of arrays of strings to basically hold historical snapshots of arrayOfStrings. My problem comes when I need to access the snapshot array for data. This is the code I have to put stuff into the snapshot array:
arrayOfArraysOfStrings[input_index] = arrayOfStrings; // you can assume input_index is updated correctly, I've already checked that.
This appears to be incorrect since when I try to access and print out the contents of the snapshot array, it only prints out the information from the most recent arrayOfStrings. The idea I was going for with this was to store the address of where arrayOfChars is pointing in an entry of the snapshot array so I can access it later.
Right now, access to an entry in the snapshot array is accomplished like this:
arrayOfArraysOfChars[historicalIndex][indexOfTargetChar]
There are several questions I'm looking to answer:
- Is the method I outlined appropriate for what I'm trying to do or is there a flaw in my overall logic?
- What am I doing wrong with my current code and how do I fix it?
- Is there a better way to do this, and if so, how does initialization of the arrays, addition to the arrays, and reading from the arrays work?
--edit 4/18-- Part of the problem is that I'm setting pointers in arrayOfArraysOfStrings to point to the same thing that arrayOfStrings is pointing to. That's bad since arrayOfStrings gets edited. I need some way to duplicate a 2D array... Preferably by simply allocating a new block of memory for arrayOfStrings to point at.