From: George Foot Newsgroups: comp.os.msdos.djgpp Subject: Re: Sizeof and pointers Date: 5 Feb 1998 08:33:19 GMT Organization: Oxford University, England Lines: 59 Message-ID: <6bbtgf$3v0$6@news.ox.ac.uk> References: <34D8C88E DOT 2849A0A AT cs DOT curtin DOT edu DOT au> NNTP-Posting-Host: sable.ox.ac.uk Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Thu, 05 Feb 1998 03:59:10 +0800 in comp.os.msdos.djgpp David Shirley wrote: : void func(unsigned char *tmp) : { : // BLAH BLAH : } : How do i find how many bytes tmp is taking up in memory: : the sizeof funtion returns 4 bytes, the reason being (i think) that it : is finding out how many bytes the pointer takes up in memory. I dont : want to use strlen(tmp) because the string may have a '\0' character. `tmp' *is* a pointer, and sizeof (tmp) is telling you exactly how big it is. What you want is a way of finding the size of the thing `tmp' points at. You can use `sizeof (*tmp)' to find this -- you'll get 1, since `tmp' points at a char. Not very useful, eh? The simple answer is that there is no way your function can know, given only the value of `tmp'. You'll have to do some better bookkeeping, I'm afraid. You imply that `tmp' is pointing at a string; if you guarrantee that the string is properly formed you won't have any problem here. If null-terminating it C-style is problematic, perhaps using a Pascal-style string (where tmp[0] is the length of the string, and tmp[1] is the first character) would work better. If it is likely not to be a proper text string (perhaps it's just a block of binary data) then you'll have to pass a second parameter to the function, giving the size of the object. Surely the caller knows how big the block is? Some part of your program must have allocated it :). As a separate note, if what is being pointed at is an array then you can find out how large it is, using `sizeof' as normal: ---- start ----- #include int array[10]; int main (void) { int a = sizeof (array); int b = sizeof (*array); printf ("sizeof (array) = %d\n", a); printf ("sizeof (*array) = %d\n", b); printf ("num of elements = %d\n", a/b); return 0; } ----- end ----- This is a pure C question, and shouldn't really have been asked on the djgpp newsgroup -- comp.lang.c would probably have been a more suitable place. -- george DOT foot AT merton DOT oxford DOT ac DOT uk Remember what happened to the dinosaurs.