Ah, pointers...
-
Chaps and fellow geeks, I have a pointer to an array of 10 chars (note that this is different from an array of ten chars, which allocates 10 bytes, the former allocates 4 bytes). This means, whenever I increment the pointer, I can skip by 10 bytes. I then proceed to allocate 100 bytes of storage (as I want to treat it as a 10 by 10 array). Trouble is (well, not really trouble), when I use new to allocate 100 bytes for the pointer I need to typecast the pointer returned by the new operator to the type of "pointer to an array". Towards this end, I have typedefed CHARTENARRAY as a pointer to an array of 10 bytes. I then typecast the return value of new like so: pArray = (CHARTENARRAY) new char[100]; I would like to know if there is a way of doing this *without* the typedef. How would I typecast it? In general, what would be the cast for a pointer to an array of x bytes that is returned, by the new operator when it is used to allocate x bytes? To make things clear, here is the source (with lots of comments), you can compile it and run it if you'd like to experiment: typedef char (*CHARTENARRAY)[10]; // typedefines CHARTENARRAY to be a pointer to an array of 10 b CHARTENARRAY pArray = NULL; pArray = (CHARTENARRAY) new char[100]; // need to do this WITHOUT the (CHARTENARRAY) typedef. for(int i = 0; i < 10; ++i) { strcpy(*pArray, "HELLO!"); // Fill in 10 "HELLO!" strings in 10 slots ++pArray; // increment by 10 bytes (scalar) } pArray -= 10; // Go to array start (will decrement by 100 bytes, since scalar is 10) for(i = 0; i < 10; ++i) // Display routine { puts(*pArray); ++pArray; // Go to next string } pArray -= 10; // Go to array start delete [] pArray; // Free
-
Chaps and fellow geeks, I have a pointer to an array of 10 chars (note that this is different from an array of ten chars, which allocates 10 bytes, the former allocates 4 bytes). This means, whenever I increment the pointer, I can skip by 10 bytes. I then proceed to allocate 100 bytes of storage (as I want to treat it as a 10 by 10 array). Trouble is (well, not really trouble), when I use new to allocate 100 bytes for the pointer I need to typecast the pointer returned by the new operator to the type of "pointer to an array". Towards this end, I have typedefed CHARTENARRAY as a pointer to an array of 10 bytes. I then typecast the return value of new like so: pArray = (CHARTENARRAY) new char[100]; I would like to know if there is a way of doing this *without* the typedef. How would I typecast it? In general, what would be the cast for a pointer to an array of x bytes that is returned, by the new operator when it is used to allocate x bytes? To make things clear, here is the source (with lots of comments), you can compile it and run it if you'd like to experiment: typedef char (*CHARTENARRAY)[10]; // typedefines CHARTENARRAY to be a pointer to an array of 10 b CHARTENARRAY pArray = NULL; pArray = (CHARTENARRAY) new char[100]; // need to do this WITHOUT the (CHARTENARRAY) typedef. for(int i = 0; i < 10; ++i) { strcpy(*pArray, "HELLO!"); // Fill in 10 "HELLO!" strings in 10 slots ++pArray; // increment by 10 bytes (scalar) } pArray -= 10; // Go to array start (will decrement by 100 bytes, since scalar is 10) for(i = 0; i < 10; ++i) // Display routine { puts(*pArray); ++pArray; // Go to next string } pArray -= 10; // Go to array start delete [] pArray; // Free
Don't try to do it without the typedef. Using the typedef makes the code easier to understand. Well, ok, to answer, I think the cast would be (char (*)[10]). But honestly, only Bjarne Stroustroup would know what that means. ;) Stick with the typedef. --Mike-- http://home.inreach.com/mdunn/ #include "buffy_sig"
-
Don't try to do it without the typedef. Using the typedef makes the code easier to understand. Well, ok, to answer, I think the cast would be (char (*)[10]). But honestly, only Bjarne Stroustroup would know what that means. ;) Stick with the typedef. --Mike-- http://home.inreach.com/mdunn/ #include "buffy_sig"
Won't forget that in a hurry-it works, thanx Mike. I did try (char*)[10] - this didn't work, though I thought that was intuitive :) Looks like the right to left rule of reading declarations might come in handy even in areas such as this! Cheers, Shanker.