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

Anand R anand.r at cybertech.co.in
Fri Aug 16 18:55:34 EST 2002


Thanks Laura/Peter/Suzi, that was really helpful..I managed to get some code
on this from a friend of mine...She showed me, how I had messed up. And
where was the code going wrong. I was actually attempting to write my own
strrev() function, using pointers. Got it finally. I have to sit with my
friend and understand the code. Will post the code once I finish.
Thanks for everything. 

regards,
--Andy

-----Original Message-----
From: Laura Bowser [mailto:lbowser at andrew.cmu.edu]
Sent: Tuesday, August 13, 2002 7:03 PM
To: Anand R; courses at linuxchix.org
Subject: Re: [Courses] [C] What is wrong here??


>
>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