jseidman@jarthur.Claremont.EDU (Jim Seidman) (05/18/91)
I ran into this using Microsoft C 6.00A, and wanted to know if this was
a bug in MSC or the way an ANSI C compiler really should work. Consider
the following function:
void test(char param[80]) {
char local[80];
printf("sizeof(param) = %d\n", sizeof(param));
printf("sizeof(local) = %d\n", sizeof(local));
}
Now, I thought that this should print out the same size for both, namely
80. But according to MSC, local has a size of 80, and param has a size
of 2 (the size of a pointer).
The ANSI K&R doesn't address this issue explicitly. Any definitive
answers on what should happen?
--
Jim Seidman, Headland Technology, 46221 Landing Parkway, Fremont CA 94538
"It doesn't need to work. They'll be paralyzed laughing at me."
- The Doctor, "Shada"
darcy@druid.uucp (D'Arcy J.M. Cain) (05/19/91)
In article <12151@jarthur.Claremont.EDU> Jim Seidman writes: >void test(char param[80]) { > char local[80]; > > printf("sizeof(param) = %d\n", sizeof(param)); > printf("sizeof(local) = %d\n", sizeof(local)); >} >Now, I thought that this should print out the same size for both, namely >80. But according to MSC, local has a size of 80, and param has a size >of 2 (the size of a pointer). The assumption that you seem to be making here is that the parameter param is the entire array. Arrays are not passed in C but rather a pointer to the initial element of the array. The compiler treats the above as: void test(char *param) and if you read it that way the above makes perfect sense. -- D'Arcy J.M. Cain (darcy@druid) | D'Arcy Cain Consulting | There's no government Toronto, Ontario, Canada | like no government! +1 416 424 2871 |
ins845b@monu4.cc.monash.edu.au (mr k.l. lentin) (05/19/91)
In article <12151@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (Jim Seidman) writes: > >I ran into this using Microsoft C 6.00A, and wanted to know if this was >a bug in MSC or the way an ANSI C compiler really should work. Consider >the following function: > >void test(char param[80]) { > char local[80]; > > printf("sizeof(param) = %d\n", sizeof(param)); > printf("sizeof(local) = %d\n", sizeof(local)); >} > >Now, I thought that this should print out the same size for both, namely >80. But according to MSC, local has a size of 80, and param has a size >of 2 (the size of a pointer). > >The ANSI K&R doesn't address this issue explicitly. Any definitive >answers on what should happen? > C passes all arrays as pointers and as a stringis an array, it is passed as a pointer. I am not sure but sizeof(*param) MIGHT return 80 but sizeof(param) should return 2 as I can see it. |/ |\evin
rearl@watnxt3.ucr.edu (Robert Earl) (05/20/91)
In article <1991May19.135611.6332@monu0.cc.monash.edu.au> ins845b@monu4.cc.monash.edu.au (mr k.l. lentin) writes: | In article <12151@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (Jim Seidman) writes: | > | >void test(char param[80]) { | > char local[80]; | > | > printf("sizeof(param) = %d\n", sizeof(param)); | > printf("sizeof(local) = %d\n", sizeof(local)); | >} | > | C passes all arrays as pointers and as a stringis an array, it is passed | as a pointer. I am not sure but sizeof(*param) MIGHT return 80 but | sizeof(param) should return 2 as I can see it. No, since `param' has decayed to type `char *', sizeof(*param) returns sizeof(char), which is 1. -- ______________________________________________________________________ \ robert earl / "Love is a many splintered thing" rearl@watnxt3.ucr.edu \ --Sisters of Mercy rearl@gnu.ai.mit.edu /
gwyn@smoke.brl.mil (Doug Gwyn) (05/20/91)
In article <12151@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (Jim Seidman) writes:
-void test(char param[80]) {
- char local[80];
- printf("sizeof(param) = %d\n", sizeof(param));
- printf("sizeof(local) = %d\n", sizeof(local));
-}
-Now, I thought that this should print out the same size for both, namely
-80. But according to MSC, local has a size of 80, and param has a size
-of 2 (the size of a pointer).
MSC is right. The parameter DOES have type pointer-to-char; you've
just written in in an opaque way.
worley@compass.com (Dale Worley) (05/20/91)
In article <12151@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (Jim Seidman) writes: void test(char param[80]) { char local[80]; } Yes, the type of 'param' is 'char *'. See section 3.7.1 of the ANSI standard. This makes is hard to pass arrays as arguments. However, if you wrap the array in a struct, the array will be passed as requested! struct dummy { char a[80]; }; void test(struct dummy param) { printf("sizeof(param) = %d\n", sizeof(param)); } will print 80 as expected! Dale Dale Worley Compass, Inc. worley@compass.com -- Q: Why does Internet raise issues of academic freedom and free speech? A: Anything you can write publish, broadcast, or yell from the top of a building, Internet can propagate further, faster, more elegantly. Thus, age-old issues of freedom of speech and expression vs. public safety and moral sensibilities have moved into this high tech arena. -- Joe Abernathy [And in an intensified form.]
torek@elf.ee.lbl.gov (Chris Torek) (05/23/91)
>In article <12151@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (Jim Seidman) asks why `sizeof param' is 2 rather than 80 in: >>void test(char param[80]) { on his particular machine. In article <1991May19.135611.6332@monu0.cc.monash.edu.au> ins845b@monu4.cc.monash.edu.au (mr k.l. lentin) writes: >C passes all arrays as pointers and as a stringis an array, it is passed >as a pointer. More precisely, a pointer to the first element of the array. There is one major Rule about arrays in C, from which many lesser rules follow. The Rule is this: In any value context, an object of type `array N of T' is converted to a value of type `pointer to T' by locating the first element of that array. All function parameters are value contexts, and all array objects are `objects of type ``array N of T'' '---here param is an object of type `array 80 of char'---and this means that there are no array parameters. When you place an array-80-of-char in a parameter position it is changed to a `pointer to char', pointing to the first character of the array. So, you write: I have this function called test. It takes one parameter, which is an array of 80 `char's. The function has no return value. The compiler reads this and thinks[%], `Array? There ain't no arrays here. You must've meant that you put an array-80-char in as an argument, and that's been converted to a pointer to char, so I'll just *pretend* that that's what you wrote.' The compiler pretends so thoroughly that it later thinks you gave it: void test(char *param) { and thus `sizeof(param)' is equal to `sizeof(char *)' and `sizeof(*param)' is equal to sizeof(char) (which is always 1). ----- [%] Anthropomorphic analogies, always available, are abilitating, although also aromatic. :-) -- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427) Berkeley, CA Domain: torek@ee.lbl.gov
mouse@thunder.mcrcim.mcgill.edu (der Mouse) (05/25/91)
In article <WORLEY.91May20111036@sn1987a.compass.com>, worley@compass.com (Dale Worley) writes: > struct dummy { char a[80]; }; > void test(struct dummy param) { > printf("sizeof(param) = %d\n", sizeof(param)); > } > will print 80 as expected! Or possibly more, if the compiler chooses to pad the structure :-) (For example, if every object is rounded up to a power of two, as I recall someone, ages ago, saying some twisted machine did....) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu