Mail Archives: djgpp/1998/02/05/04:32:14
On 5 Feb 98, David Shirley was found to have commented thusly:
> Hi,
>
> Ok heres my problem,
>
> 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.
>
> Can someone tell me how to find out how many bytes tmp takes up in
> memory, OR how to declare it so that sizeof(tmp) will return the correct
> number?
Here is something you can do at home but only with adult supervision
(I am kidding, of course):
#include <stdio.h>
#include <string.h>
int main()
{
char p[10] = "My string", *tmp;
int i;
tmp = p;
printf("\nsizeof(tmp) = %d", sizeof(tmp));
printf("\nsizeof(*tmp) = %d", sizeof(*tmp));
printf("\nsizeof(p) = %d", sizeof(p));
printf("\nstrlen(p) = %d", strlen(p));
printf("\nstrlen(tmp) = %d", strlen(tmp));
return (0);
}
It even compiled and ran.
--output---
sizeof(tmp) = 4
sizeof(*tmp) = 1
sizeof(p) = 10
strlen(p) = 9
--end of output---
I will let you draw your own conclusions. By the way, I also
declared array p with empty brackets, i.e., as "char p[] =..."
It also compiled and ran, although I believe that the ANSI/ISO
standard has some nuisance rule that says that compilers should
report an error for local arrays whose size is not defined...but
what do I really know?
Mitch Halloran
Research (Bio)chemist
Duzen Laboratories Group
Ankara TURKEY
mitch AT duzen DOT com DOT tr
other job title: Sequoia's (dob 12-20-95) daddy
- Raw text -