[comp.lang.c] another stupid array/pointer question

freedman@granite.cr.bull.com (Jerome Freedman) (03/02/90)

Suppose I have a structure

    struct a {
       char * a_string;
    }

and a structure

    struct b {
	char another_string[80];
    }

 sizeof(struct a) should equal sizeof(char *)

 but what about sizeof(struct b)?
 Is the 80 character array included in the size of struct b or
 is b.another_string a pointer into somewhere  where there is
 allocated space - is this implementation defined?

  I realize this might be a "dumb" question but not as dumb as if
I didn't ask it.

                                     Jerry Freedman, Jr
  

dkim@wam.umd.edu (Daeshik Kim) (03/03/90)

In article <1990Mar2.135645.17274@granite.cr.bull.com> freedman@granite.cr.bull.com (Jerome Freedman) writes:
>
>Suppose I have a structure
>
>    struct a {
>       char * a_string;
>    }
>
>and a structure
>
>    struct b {
>	char another_string[80];
>    }
>
> sizeof(struct a) should equal sizeof(char *)
>
> but what about sizeof(struct b)?
> Is the 80 character array included in the size of struct b or
> is b.another_string a pointer into somewhere  where there is
> allocated space - is this implementation defined?
>
	As far as I know, 'struct a' only has a space for 'ptr';
	but 'struct b' holds 80 bytes statically allocated.

	In most c-compilers, I would say that
		if you have:
			struct b str; /* now, allocated. */
		if you refer str, the address of 'str' should be
		same as the address of 'str.another_string[0]',
		which means that str and str.another_string points
		same location.

	I haven't seen c-compilers with other strategy yet.
--
	Daeshik Kim	H: (301) 445-0475/2147 O: (703) 689-5878
	SCHOOL:	dkim@wam.umd.edu, dskim@eng.umd.edu, mz518@umd5.umd.edu
	WORK:	dkim@daffy.uu.net (uunet!daffy!dkim)

henry@utzoo.uucp (Henry Spencer) (03/03/90)

In article <1990Mar2.135645.17274@granite.cr.bull.com> freedman@granite.cr.bull.com (Jerome Freedman) writes:
>    struct b {
>	char another_string[80];
>    }
>
> Is the 80 character array included in the size of struct b ...

Yes.  There is little "invisible machinery" in C; in general, you get
what you ask for.  In this case, you've asked for a struct containing
an 80-character array.
-- 
MSDOS, abbrev:  Maybe SomeDay |     Henry Spencer at U of Toronto Zoology
an Operating System.          | uunet!attcan!utzoo!henry henry@zoo.toronto.edu