Sponsored Content
Full Discussion: C Recursion (explain)
Top Forums Programming C Recursion (explain) Post 302416390 by jim mcnamara on Monday 26th of April 2010 10:29:59 AM
Old 04-26-2010
Code:
void printit(char line_of_char[], int index)
{
 if(line_of_char[index])
 {
  index++;
  printf("+%c", line_of_char[index]); /*how come this is not being printed after "-" was printed*/
  printit(line_of_char, index);
  printf("-%c", line_of_char[index]);
 }
}

function call pushes data onto the stack, so what this does is to push various
values of index onto the stack. The first print statement is using the value of index as it gets incremented 1,2,3 ...

The second printf (after the recursive call) statement is going "back in time" by popping values off the stack 10,9,8, .....

Change the code so it just prints value of the index variable, nothing else. Then you will see.

BTW the "if(line_of_char[index])" has a problem, the initial value is -1, which is the character before the start of the variable line_of_char. Apparently it works for you, but that it called "programming by coincidence" a really bad thing. It could trigger a segfault on other systems. Or other problems.

---------- Post updated at 08:29 ---------- Previous update was at 08:16 ----------

Corrected code, sort of:
Code:
#include <stdio.h>
#include <string.h>

void printit(char line_of_char[], int index);
int main()
{
   char line_of_char[80]={0x0};
   int index = -1;
   
   strcpy(line_of_char, "This is a string.");
   printit(line_of_char, index);
   printf("\n");
   return 0;
}
void printit(char line_of_char[], int index)
{
   if(index ==-1 || line_of_char[index])
   {   
   	index++;
    printf("+%c", line_of_char[index]); 
    printit(line_of_char, index);
    printf("-%c", line_of_char[index]);
   }
}


Last edited by jim mcnamara; 04-26-2010 at 11:23 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies

2. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

3. Shell Programming and Scripting

Problem with recursion in subdirectories

Hello ! I need some help with my simple bash script. This script removes all files ( with name given in $1 ) in current dir and subdirectories . The problem is with first loop in the script ( for file in * ; do ) . When I run the sript in my home directory this script display sometimes( ... (5 Replies)
Discussion started by: scotty_123
5 Replies

4. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

5. Shell Programming and Scripting

diffrence in non recusion and recursion

Hi, If i have given to write a prog for factorial in C using recursion and without recursion which one is better in what condition and why ? thanks (2 Replies)
Discussion started by: useless79
2 Replies

6. Shell Programming and Scripting

How to remove old files without recursion?

Hi folks, I need to write a script which remove files with suffix *.dmp from a specific directory ,older than 30 days and not including recursive subdirectories. I.e: The following command remove recursive all *.dmp files older than 30 days: find $ORACLE_BASE -mtime +30 -type f -name... (5 Replies)
Discussion started by: nir_s
5 Replies

7. Shell Programming and Scripting

KSH: recursion into subdirectories?

Hello, I'm scripting a newbie. I'm using KSH on HP-UX. I'm trying to write a script that will change a whole directory of file names into UPPER CASE. I have the "convert to upper case" part of it working fine: ls | while read filename; do typeset -u uppercase uppercase=${filename} ... (2 Replies)
Discussion started by: Wotan31
2 Replies

8. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

9. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

10. Solaris

BIND 9, disable recursion

Hi, I am trying to disable the recursion on DNS server (Solaris 10). I have added the lines in the named.conf as below: allow-query-cache { none; }; recursion no; Then restarted the solaris DNS services svcadm refresh svc:/network/dns/server:default Still I am able to... (0 Replies)
Discussion started by: snchaudhari2
0 Replies
SSL_get_ex_data_X509_STORE_CTX_idx(3SSL)			      OpenSSL				  SSL_get_ex_data_X509_STORE_CTX_idx(3SSL)

NAME
SSL_get_ex_data_X509_STORE_CTX_idx - get ex_data index to access SSL structure from X509_STORE_CTX SYNOPSIS
#include <openssl/ssl.h> int SSL_get_ex_data_X509_STORE_CTX_idx(void); DESCRIPTION
SSL_get_ex_data_X509_STORE_CTX_idx() returns the index number under which the pointer to the SSL object is stored into the X509_STORE_CTX object. NOTES
Whenever a X509_STORE_CTX object is created for the verification of the peers certificate during a handshake, a pointer to the SSL object is stored into the X509_STORE_CTX object to identify the connection affected. To retrieve this pointer the X509_STORE_CTX_get_ex_data() function can be used with the correct index. This index is globally the same for all X509_STORE_CTX objects and can be retrieved using SSL_get_ex_data_X509_STORE_CTX_idx(). The index value is set when SSL_get_ex_data_X509_STORE_CTX_idx() is first called either by the application program directly or indirectly during other SSL setup functions or during the handshake. The value depends on other index values defined for X509_STORE_CTX objects before the SSL index is created. RETURN VALUES
>=0 The index value to access the pointer. <0 An error occurred, check the error stack for a detailed error message. EXAMPLES
The index returned from SSL_get_ex_data_X509_STORE_CTX_idx() allows to access the SSL object for the connection to be accessed during the verify_callback() when checking the peers certificate. Please check the example in SSL_CTX_set_verify(3), SEE ALSO
ssl(3), SSL_CTX_set_verify(3), CRYPTO_set_ex_data(3) 1.0.1e 2013-02-11 SSL_get_ex_data_X509_STORE_CTX_idx(3SSL)
All times are GMT -4. The time now is 04:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy