[Courses] re: [C] lesson 7

Laurel Fan laurel at sdf.lonestar.org
Tue Nov 5 11:29:39 EST 2002


I don't believe this will work, see my comments below.

On Sun, Nov 03, 2002 at 04:29:39PM -0500, Morgon Kanter wrote:
> char consonent_or_vowel(char letter) {

You declared the function as returning a character.

>  char returnchar[10];

However, you actually try to return an array of characters, which is
not the same thing.  When I try to compile it in gcc, I get this
warning:

test.c:12: warning: return makes integer from pointer without a cast

In general, this warning means you may not be returning the same type
that the function claims to.

>  switch (letter) {
>   case a:

I think you mean
    case 'a':
    case 'A':

etc...

>   case A:
>   case e:
>   case E:
>   case i:
>   case I:
>   case o:
>   case O:
>   case u:
>   case U:
>    returnchar = "consonent";

Also, you cannot assign a string constant ("consonent") to an array of
char (returnchar).

You can assign a string constant to a char *, like:

char *function1()
{
   char *return_value1;
   return_value1 = "consonent";
   return return_value1; /* ok */
}

This works fine.

You can also copy a string constant into an array of char, like:

char *function2()
{
   char return_value2[10];
   strcpy(return_value2, "consonent");
   return return_value2; /* bad */
}

However, you should not return return_value2 from a function.  The
reason for this is that return_value2 is a local variable.  It only
exists in the function, and once you return from the function, the 10
characters in return_value do not exist.  gcc will in fact warn you if
you try to do that:

test.c:12: warning: function returns address of local variable

The reason function1 works is that string constants (like "consonent")
are not local to the function, so they do not go away when you return
from the function.

>    break;
>   default:
>    returnchar = "vowel";
>    break;
>   }
>  return returnchar;
> }

There are two ways I might rewrite this:

/* This version returns the string constant "vowel" if the letter is a
vowel, and returns the string constant "consonant" if the letter is a
consonant. */
char *consonant_or_vowel(char letter)
{
    char *return_string;
    switch(letter) {
        case 'a':
        case 'A':
        /* etc... */
            return_string = "vowel";
            break;
        default:
            return_string = "consonant";
            break;
    }
    return return_string;
}

/* This version returns 1 if the letter is a vowel, and 0 if the
letter is a consonant.  Sometimes not dealing with strings can
simplify code. */

int isvowel(char letter)
{
    int return_value;
    switch(letter) {
        case 'a':
        case 'A':
        /* etc... */
            return_value = 1; /* vowel */
            break;
        default:
            return_value = 0; /* consonant */
            break;
    }
    return return_value;
}

The differences between char, char[], char*, and string constants in C
can be particularly confusing to beginners (especially if they already
know a language where strings are more natural).  Let me know if this
explanation makes sense.

-- 
laurel at sdf.lonestar.org
http://dreadnought.gorgorg.org



More information about the Courses mailing list