www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/05/04:32:14

Message-Id: <199802050928.LAA10310@ankara.duzen.com.tr>
Comments: Authenticated sender is <mitch AT ankara DOT duzen DOT com DOT tr>
From: "S. M. Halloran" <mitch AT duzen DOT com DOT tr>
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
Subject: Re: Sizeof and pointers
In-reply-to: <34D8C88E.2849A0A@cs.curtin.edu.au>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019