Charles Mills wrote:


I wonder how C passes a potentially 16-byte string by value.

Not sure what you mean by that question... a C string constant
is an array of characters; otherwise a C 'string' is typically
a pointer to a character or a declared array of characters.

When you pass an array as a parameter; it is converted to
a pointer to the first element and the pointer is passed.

When you pass a pointer - a pointer is passed.

For example:

  myfunc("A string");

invokes the function 'myfunc' with the address of the array of
nine characters defined as { 'A', ' ', 's', 't', 'r', 'i', 'n', 'g', '\0' }.

And - myfunc would typically be declared/defined as

 void myfunc(char *);

Note that if you declare myfunc this way:

  void myfunc(char [9]);

because you are declaring an array as the parameter, the parameter
value is treated as a pointer.  That is, even with that declaration, C would
pass a pointer to the first element of the array.

Why is this?

Because of the C rule that an array name is converted to the address of
the first element of the array, and that subscripting operations then devolve
into pointer arithmetic.

For example:

   int array[10];

   array[5] = 5;

The expression 'array[5]' is completely the same as

  (&array[0] + 5)

and that's what the C language actually does...

Note that since [] is actually just an operator in C, you can
code the equivalent:

  5[array];

which says "add 5 to the address of the first element of 'array'".

It's a little confusing to write it that way - but that's the same semantics
as "add the address of the first element of 'array' and 5" since pointer
addition is commutative.

(Try it :-) )

The only way to pass a "string by value" would be to
declare an array inside a structure, and pass the structure
by value... which is well-defined.  That is, at that point you
are passing a structure, not an array (the structure happens to
contain an array...)

  - Dave Rivers -

--
riv...@dignus.com                        Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to