You are here:

C/C program meaning - reverse string...

Advertisement


Question
How this code works? It's suppose to reverse the string I input. But I just found an infinite loop in the reverse function... or i'm wrong? Can i know brief about pointers and c functions... I also don't understand why there are two const in the const char * const sPtr... Is it not suppost to be const char *sPtr; ? Just a favor to ask... where can i get more tutorial or question and solution about the pointers and c libraries?

-------------------------------------------------

#include<stdio.h>

void reverse( const char * const sPtr );

int main(){
char sentence[ 80 ];

printf( "Enter a line of text:\n" );
gets( sentence );

printf( "\nThe line printed backwards is:\n" );
reverse( sentence );
}

void reverse( const char * const sPtr ){
if( sPtr[ 0 ] == '\0' ){
       return;
}
else{
reverse( &sPtr[ 1 ] );

putchar( sPtr[ 0 ] );
}
}

-------------------------------------------------

Answer
Here are some of the good C tutorials that I know of:
http://www.geocities.com/SiliconValley/Software/5562
http://www.tgr.com/weblog/archives/000028.html
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/toc.html

>I also don't understand why there are two const in the const char * const sPtr...
In char *sPtr; there are two things.
One is the variable sPtr and another is the location which is pointed to by the address stored in sPtr.
Suppose sPtr is stored at the address 1234.
And the contents of sPtr is 5678.
You can make any of these two or both as constant (readonly).
When you say const char *sPtr;
Only the contents of address pointed by sPtr is made readonly.
When you say char * const sPtr;
Only the contents of sPtr is made readonly.
To make both readonly, you will have to declare:
const char * const sPtr;
With this you cannot change the adrress stored in sPtr (in our case it is 5678). And also the value stored at the place 5678 cannot be changed.

I tested your code and it is working fine:-)
The logic is, in the line:
reverse( &sPtr[ 1 ] );
You are passing the address of the 2nd character to the function, during first pass.
When it enters the funciton, now this 2nd character has become the first character!
And the statement:
reverse( &sPtr[ 1 ] );
will send the address of the 3rd character.
This goes on till the end.
And in the end, the condition:
if( sPtr[ 0 ] == '\0' ){
becomes true and it returns.

So, there is no infinite loop.

It is not easy to understand a recursive function.
You will have to take a sample input and execute each step without assuming anything. Then only you will be able to debug it.

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.