Is it possible to get the size of an array through a pointer? ie:
void pointertest2(CHAR_DATA *ch, char *test)
{
ch_printf(ch, " %d\n\r", sizeof(test));
}
CMDF do_pointertest(CHAR_DATA *ch, char *argument)
{
char buf1[MSL];
pointertest2(ch,buf1);
}
Also, is there a way to do this with references without changeing the call to the function, only changing the function itself?
Probably not portably. There is a function mentioned in places, msize, that returns the size of your block.
eg.
int i = msize (test);
However I can compile but not link that. I don't think it is standard C.
In fact in your example even that would not work. The msize function I mentioned returns the size of a block on the *heap*, that is if you do malloc. However your example only sends the pointer to a block which is a local piece of data.
There is no way that a pointer to a block of memory like that can somehow know how long it is. However you might do it with some defines, if you don't mind changing the calling function, eg. by using sizeof.
So you could make a define:
#define pointertest2(ch, buf) call_pointertest2 (ch, buf, sizeof buf)
This effectively calculates the size and then calls another function.
I was considering that myself, only problem I came across was if I was passing something that was already a pointer, sizeof returns 4. I dunno, guess I'll probably have to do it all by hand.
Yes, you have to use sizeof on the actual array, not the pointer to the array.