[comp.lang.c] total space of a string

maart@cs.vu.nl (Maarten Litmaath) (12/20/88)

barmar@think.COM (Barry Margolin) writes:
\1) When I've done this in other languages, I've used something like
\strlen("/") instead of the 2.  Unfortunately, in C I'd still have to
\say "+1", which I'd then want to comment with /* leave room for the
\trailing null */, since I don't think there's an expression that will
\return the total space taken up by a string.

How about the following?

	sizeof "/"
-- 
fcntl(fd, F_SETFL, FNDELAY):          |Maarten Litmaath @ VU Amsterdam:
      let's go weepin' in the corner! |maart@cs.vu.nl, mcvax!botter!maart

kyriazis@rpics (George Kyriazis) (12/21/88)

In article <1827@solo11.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>barmar@think.COM (Barry Margolin) writes:
>\1) When I've done this in other languages, I've used something like
>\strlen("/") instead of the 2.  Unfortunately, in C I'd still have to
>\say "+1", ....
>
>How about the following?
>
>	sizeof "/"

"/" is a pointer to a string containing a '/' and a '\0'.  Therefore
it seems to me that sizeof "/" will return the same value as 
sizeof( char * ).  
Comments??


  George Kyriazis
  kyriazis@turing.cs.rpi.edu
  kyriazis@ss0.cicg.rpi.edu
------------------------------

guy@auspex.UUCP (Guy Harris) (12/22/88)

>"/" is a pointer to a string containing a '/' and a '\0'.

"/" is not a pointer to a string; it is the string (i.e. an array of
"char") itself.  In some, but not all, contexts, the array is converted
to a pointer to its first member; "argument of 'sizeof'" is not one of
those contexts.  (Check your K&R, or your dpANS.)

>Therefore it seems to me that sizeof "/" will return the same value as 
>sizeof( char * ).  

This is, alas, apparently a mistake made by some C implementors; with
any luck, there'll be a conformance-testing suite for the ANS when it
comes out, and it will catch errors such as that. 

poek@psu-cs.UUCP (Ken Poe) (12/30/88)

In article <29@rpi.edu> > kyriazis@rpics (George Kyriazis) writes:

>In article <1827@solo11.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
>>barmar@think.COM (Barry Margolin) writes:
>>\1) When I've done this in other languages, I've used something like
>>\strlen("/") instead of the 2.  Unfortunately, in C I'd still have to
>>\say "+1", ....
>>
>>How about the following?
>>
>>	sizeof "/"
>
>"/" is a pointer to a string containing a '/' and a '\0'.  Therefore
>it seems to me that sizeof "/" will return the same value as 
>sizeof( char * ).  
>Comments??

	Only if your system has 2 byte pointers.

	The sizeof operator in C returns the size of the object (or of 
	any object of that type for size of a type).  When the object 
	is a "string" (actually an array of char), the sizeof operator
	returns the number of bytes in the array. (including the null
	character ('\0') at the end of the "string") 

	Try the following:

		main()
		{
			static char string[] = "abcd";
			/* null char appended to string constants by the compiler */

			printf("The size of string is %d\n", sizeof(string) );
			printf("The size of \"/\" is %d\n", sizeof "/");
			printf("The size of (char *) is %d\n", sizeof (char *) );
		}

	On the VAX it's output is:

		The size of string is 5
		The size of "/" is 2
		The size of (char *) is 4

	The only thing that is implementation dependent is the size of
	a pointer (32 bits on most systems) which can be 2 bytes.  (near
	pointers in MSC etc).

-- 
   Kenneth N. Poe ATP, CFI  |  ARPANET: poek%cs.pdx.edu@relay.cs.net
   16925 N.E Glisan St.     |  CSNET:   poek@cs.pdx.edu
   Portland OR  97230-6252  |  UUCP:    {ucbvax,uunet,gatech}
   voice: (503) 252-1191    |             !tektronix!psu-cs!poek
   cu:    (503) 254-1009    |  C-SERVE: 73477,2452 
   (when I get it working)

    ----------------------------------------------------------------------  
    I would much rather be on the ground wishing I were in the air than
    to be in the air wishing I were on the ground.  --  Ken Poe
    ----------------------------------------------------------------------