Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ pointers to arrays

pointers to arrays

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Greven   Canada  (835 posts)  Bio
Date Sun 25 Jul 2004 06:54 PM (UTC)
Message
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?

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 25 Jul 2004 11:05 PM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Greven   Canada  (835 posts)  Bio
Date Reply #2 on Sun 25 Jul 2004 11:22 PM (UTC)
Message
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.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 27 Jul 2004 03:26 AM (UTC)
Message
Yes, you have to use sizeof on the actual array, not the pointer to the array.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,082 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.