Thursday, 5 April 2012

Passing An Array to Functions in C & C++


The previous entry looked at various ways to pass a variable to a function: by value, by reference or by pointer. This entry looks at passing an array to a function. In C/C++ arrays are passed by reference or by pointer, not by value. This means the called function can directly change the array as passed by the caller. You can however pass one element of an array by value in the same way you pass an integer or other data type.

The sample code calls a function, CapFirstLetter, which takes a character array from the caller and raises the first letter of each word to uppercase.

The character array is created by:

     char Text[] = "Some words to cap.";

I can then define the function signature in one of two ways: with or without a pointer.

int CapFirstLetter(char vText[]);
int CapFirstLetter(char * vText);

The code inside the function is the same.

I can then call the function in one of three ways:

CapFirstLetter(Text);

CapFirstLetter(&Text[0]);

char * pText = &Text[0];
OR
char * pText = Text;
CapFirstLetter(pText);

The critical point to remember is that using the name of the array, in this case, Text, is actually a pointer to the first element of the array. Thus, using Text is the same as using &Text[0] and a pointer can be assigned using either method. The result is the same.

Sample Code.

// Standard I/O library.
#include <cstdio>

// Prototypes.
int CapFirstLetter(char vText[]);
int CapFirstLetter2(char * vText);

int main()
{

     printf("Call Function To Cap First Letter of Every Word\n\n");

     /////////////////////////////////////////////////////////////////
     // 1. Pass array by array name.

     printf("1. Pass array by array name.\n\n");
     char Text[] = "Some words to cap.";
     CapFirstLetter(Text);
     printf("Version 1: %s\n", Text);
    
     // The name of the array is a pointer to first element.
     char Text2[] = " This   function   handles extra spaces.";
     CapFirstLetter(&Text2[0]);
     printf("Version 2: %s\n\n\n", Text2);

     /////////////////////////////////////////////////////////////////
     // 2. Pass array by pointer variable.

     printf("2. Pass array by pointer variable.\n\n");
     // Create pointer.
     char Text3[] = "a statement with all lowercase letters";

     // Option 1.
     // The name of the array, Text3, is a pointer to Text3[0].
     // char * pText = Text3;

     // Option 2.
     // Another way to get the address of the first element.
     char * pText = &Text3[0];
     CapFirstLetter(pText);
     printf("Version 3: %s\n", Text3);
    
     // Start later in the array.
     char Text4[] = "a statement with all lowercase letters";
     pText = &Text4[2];
     CapFirstLetter(pText);
     printf("Version 4: %s\n", Text4);

     // Close the file.
     fclose(stdout);

     // Return something.
     return 0;
}
int CapFirstLetter(char vText[])
{
     /////////////////////////////////////////////////////////////////
     // Capitalize first letter of a string of text.

     short i;
     bool PrevSpace = true;

     for (i = 0; vText[i]; i++)
     {
           // Look to find start of new word.
           if (PrevSpace)
           {
                // Ignore space.
                if (vText[i] == ' ')
                     // Go to next char.
                     continue;
                // Not a space, reset flag.
                PrevSpace = false;
                // Want lowercase only.
                if ((vText[i] >= 97) && (vText[i] <= 122))
                {
                     // Appy uppercase.
                     vText[i] -= 32;
                }
           }
           else
           {
                // Start of new word.
                if (vText[i] == ' ')
                     // Reset.
                     PrevSpace = true;
           }
     }

     // Return success.
     return 0;
}

Output.


No comments:

Post a Comment