Message-Id: <199802050928.LAA10310@ankara.duzen.com.tr> Comments: Authenticated sender is From: "S. M. Halloran" Organization: User RFC 822- and 1123-Compliant To: djgpp AT delorie DOT com Date: Thu, 5 Feb 1998 11:30:00 +0200 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: Re: Sizeof and pointers In-reply-to: <34D8C88E.2849A0A@cs.curtin.edu.au> Precedence: bulk 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 #include 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