Wednesday, September 29, 2010

Calling Functions with Arrays

Arrays are covered in detail in Chapter 4. However, this section discusses passing
arrays as arguments to functions because it is an exception to the normal call-by-value
parameter passing.
When an array is used as a function argument, its address is passed to a function.
This is an exception to the call-by-value parameter passing convention. In this case, the
code inside the function is operating on, and potentially altering, the actual contents of
the array used to call the function. For example, consider the function print_upper() ,
which prints its string argument in uppercase:
#include <stdio.h>
#include <ctype.h>
void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf("\ns is now uppercase: %s", s);
return 0;
}
/* Print a string in uppercase. */
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t) {
string[t] = toupper(string[t]);
putchar(string[t]);
}
}
After the call to print_upper() , the contents of array s in main() will change to
uppercase. If this is not what you want, you could write the program like this:
#include <stdio.h>
#include <ctype.h>

void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf("\ns is unchanged: %s", s);
return 0;
}
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t)
putchar(toupper(string[t]));
}
In this version, the contents of array s remain unchanged because its values are not
altered inside print_upper() .
The standard library function gets() is a classic example of passing arrays into
functions. Although the gets() in your standard library is more sophisticated, the
following simpler version, called xgets() , will give you an idea of how it works.
/* A simple version of the standard
gets() library function. */
char *xgets(char *s)
{
char ch, *p;
int t;
p = s; /* gets() returns a pointer to s */
for(t=0; t<80; ++t){
ch = getchar();
switch(ch) {

case '\n':
s[t] = '\0'; /* terminate the string */
return p;
case '\b':
if(t>0) t--;
break;
default:
s[t] = ch;
}
}
s[79] = '\0';
return p;
}
The xgets() function must be called with a character pointer. This, of course, can
be the name of a character array, which by definition is a character pointer. Upon entry,
xgets() establishes a for loop from 0 to 79. This prevents larger strings from being
entered at the keyboard. If more than 80 characters are entered, the function returns.
(The real gets() function does not have this restriction.) Because C/C++ has no built-in
bounds checking, you should make sure that any array used to call xgets() can accept
at least 80 characters. As you type characters on the keyboard, they are placed in the
string. If you type a backspace, the counter t is reduced by 1, effectively removing the
previous character from the array. When you press ENTER, a null is placed at the end
of the string, signaling its termination. Because the actual array used to call xgets() is
modified, upon return it contains the characters that you type.

 

No comments:

Post a Comment