0

I am trying to make a recursive function to print the content of an array. The main looks like this:

#include <stdio.h>
static int s_index;
int main(void) { int even[] = {2, 4, 6, 8}; s_index = 0; print(even);}

The print function looks like this:

void print(int * array) {
    if(s_index > 3) {
        printf("\n"); return;
    }
    printf(" %d ", *array); ++s_index; print(array + s_index);
}

What I notice is:

if &even is 0x7fffffffdbf0 then (array + s_index) increments as follow with s_index:

s_index = 0 : 0x7fffffffdbf0;
s_index = 1 : 0x7fffffffdbf4;
s_index = 2 : 0x7fffffffdbfc;

it should be 0x7fffffffdbf8!!?

It is blowing my mind, could someone help with that? Thank you for your answers.

12
  • 1
    OMG that global variable. Why not a parameter? Commented Nov 10, 2014 at 9:24
  • is you machine 32 bit or 64 bits Commented Nov 10, 2014 at 9:25
  • 2
    *array --> *(array+s_index) and print(array + s_index); --> print(array); Commented Nov 10, 2014 at 9:25
  • 5
    or, more recursiveish; print(array + 1) Commented Nov 10, 2014 at 9:26
  • @Magnus Hoff: That wouldn't work. It has to be s_index that's incremented, or the if clause will break. Commented Nov 10, 2014 at 9:32

3 Answers 3

2

You made a logical mistake in your recursive call. See what happens:

Assume &even = 0x7fffffffdbf0
First call:
array = 0x7fffffffdbf0 ; s_index = 0.
You increase s_index and pass into the second call array + s_index, which yields:
Second call:
array = 0x7fffffffdbf4 ; s_index = 1.
Again, you increase s_index and pass into the third call array + s_index, which yields:
array = (0x7fffffffdbf4) + 2 => 0x7fffffffdbfc

You should only increase array by 1 for each recursive call, and remove s_index completely (that's the point of the recursion, to get rid of global variables)

Sign up to request clarification or add additional context in comments.

1 Comment

Of course, getting rid of s_index would mean also introducing another termination criterion, for example a length parameter, and then calling print(array + 1, length - 1).
1

Please get rid of the global value s_index.Change your func signature as shown below

int main(void)
{ 
   int even[] = {2, 4, 6, 8,10,12}; 
   print(even,(sizeof(even)/sizeof(int)));
}

void print(int * array,int len) {
    if( len == 0) 
    {
        printf("\n"); return;
    }
    else
    {
    printf(" %d ", *(array)); 
    print(array+1,len-1);
    }
}

5 Comments

Thank you for the answer, indeed it is a logical mistake. However the else do not solve anything. I just changed it to print(++array).
Fixes the problem, but the global variable, although not causing anything wrong, needs to be killed with fire
@Rerito Agree but pointed out what is going wrong in the written code
@Gopi Without any explanation or advice. You should make your answer more ... enlightening :)
@Rerito there you have it :)
0

Please don't do really strange and obscure things for no apparent reason, such as using recursion where simple loops are faster, safer and more readable. Because when you are doing really strange things on purpose, other equally strange things tend to happen unexpectedly.

#include <stdio.h>

void print(int* array, size_t size) 
{
  for(size_t i=0; i<size; i++)
  {
    printf("value:%d address:%p\n", array[i], (void*)&array[i]);
  }
}

int main(void) 
{ 
  int even[] = {2, 4, 6, 8}; 
  print(even, 4);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.