[Courses] [C] What is wrong here??

Laura Bowser lbowser at andrew.cmu.edu
Tue Aug 13 09:33:04 EST 2002


>
>char strrev(char array[])
>{
>        register int i;
>        for(i=strlen(array)-1;i>=0;i--)
>        {
>                return *array;

I'm mildly suprised this compiles without warnings because you say you return 
a char, but you're returning a pointer to an array.

so you're returning a pointer to an array which is a pointer to a char
(return value) -> array -> (char) array[1] 
if you want to access the value you're returning you'd have to access it as 
&(return value).

When you're dealing with pointers (especially at first) DRAW WHAT MEMORY LOOKS 
LIKE!  It will really save you some headaches, and eventually, you'll be able 
to know what's going on without drawing the pictures.  Heck, I've been 
programming in C for almost 7 years now and I still draw pretty pictures to 
help me out :)


What looks like is happening is that you're returning the first character and 
that's it.  I think you're attempting to write a recursive function, but 
you're not quite there yet.

Hope this helps, 
Laura

-- 
Public Key available at 
http://callista.dyndns.org/~elwing/lbowser.gpg



More information about the Courses mailing list