[ale] Any C Gurus Out There
Terry Lee Tucker
terry at esc1.com
Sun Jan 25 10:21:53 EST 2004
Hello,
I have a C question. I am experiementing with pointers. I want to be able to
dynamically allocate a three dimensional array (a table of strings) and I
want to be able to be able to allocate all the parts of the array using
pointers. Yes, I know I can use subscripts and yes, I know that would be
easier to read; but, I just want to be "able" to do it. Here's what I've
discovvered so far:
char ***threeD;
threeD = (char ***) calloc (2, sizeof (char **));
This line works, and allocates space for two rows. I know tthat it works
because I have used subscripts to allocate, populate and print the contents
of the array.
I have a pointer row3d defined as:
char ***row3d;
row3d = threeD;
*row3d++ = (char **) calloc (5, sizeof (char *));
*row3d = (char **) calloc (5, sizeof (char *));
This allocates memory for the two rows and apparently is equivalent to:
threeD[0] = (char **) calloc (5, sizeof (char *));
threeD[1] = (char **) calloc (5, sizeof (char *));
This is where the problem starts. I have a pointer defined as:
char ***row0col;
row0col = threeD;
I want to use row0col to allocate memory for each column in row 0 and copy a
string to that address. I thought this would work:
**row0col = (char *) calloc (20, sizeof (char));
strcpy (**row0col, "row:0;col:0");
*(*row0col)++;
But, it doesn't. The first element, column 0, gets copied correctly but I am
unable to get the pointer to increment correctly so I can allocate a new
block of memory for the next string in column 1; I have tried many different
pointer incrementation schemes other that the above. Most don't increment it
at all. The ones that do, apparently by viewing through gdb, either increment
the address to the next character in row 0 column 0 or increment the entire
row.
I can do all of this with subscripts and it works fine. I just can't figure
out the pointer equivalent to threeD[y][x] and how to increment that with
pointer arithmetic. If anyone has a clue on this, I would appreciate your
insight. I'm sure others would as well.
Thanks..
PS: The source file is attached.
--
Quote: 59
"The concessions of the weak are the concessions of fear."
--Mark Twain
Work: 1-336-372-6812
Cell: 1-336-363-4719
email: terry at esc1.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: parse.c
Type: text/x-csrc
Size: 2107 bytes
Desc: not available
More information about the Ale
mailing list