1

Hi all C experts (please don't shoot, I'm no C programmer anymore but from time to time I have a question that pops in my mind)

I was reading another question (How to print an entered string backwards in C using only a for loop).

The "simplest" and most logical answer is

for (x = end; x >= 0; --x) {
    printf("%c", word[x]);
}

But I was wondering if there wasn't a way to achieve the same goal but staying closer to the original loop poseted:

for (x = word[end]; x >= word[0]; x--) {
    printf("%c", x);
}

I don't know enough C to work it out, but couldn't we play with the arrays pointers to loop through

char * wordp;
for(wordp = &word[end]; /*something*/; wordp--){\
   printf("%c", &wordp);
}

P.S.: I don't really care if it is a forwards or backwards loop.

P.P.S.: Sorry if I made obvious C mistakes in the pointers; point them out in the comment and I'll edit them. ;)

Jason

Community
  • 1
  • 1
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • Sure you can, and it may be more efficient since increments are faster than additions on some architectures. A good compiler should generate the same code with both approaches though. – Alexandre C. Dec 02 '10 at 22:33

4 Answers4

4

Absolutely.

char *wordp;

for(wordp = word + end; wordp >= word; wordp--){
   printf("%c", *wordp);
}
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    ... and that's how STL iterators are used – Javier Dec 02 '10 at 03:58
  • I must say I have to admire the possibilities of C :P. what I was missing is how to compare wordp >= word, I know pointers a fundamentally numbers and can be compared but I didn't know that they were necessarily ordered from smaller to bigger in a array (sounds logical thought lol). can this trick be puller with dynamical arrays/list? actually is the >= necessary ? could you simply put it == ? – Jason Rogers Dec 02 '10 at 03:58
  • @Jason: Due to the nature of the structure and the loop, we need to treat it as a boundary condition, not an event condition. – Ignacio Vazquez-Abrams Dec 02 '10 at 04:03
  • And note that you actually save one addition per iteration doing it this way ! – Alexandre C. Dec 02 '10 at 22:34
1
for (x = word[end]; x >= word[0]; x--) {
    printf("%c", x);
}

will not work, as word[end] is equivalent to *(word + end). It's already being dereferenced, so x will be set to the value of the last char, and will loop until it equals the char value of the first char. In short, it makes no sense.

Try:

char * wordp;
for(wordp = (word + end); wordp >= word; wordp--){\
   printf("%c", *wordp);
}

Remember that an array is simply a pointer to its first item.

Zack Bloom
  • 8,309
  • 2
  • 20
  • 27
1

You can do:

    char * wordp;
    for(wordp = &word[end]; wordp >= word ; wordp--){\
            printf("%c", *wordp);
    }
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Real programmers use recursion:

void revputs(char *s) {
  if (*s) { revputs(s + 1); putchar(*s); }
}
Rafe
  • 5,237
  • 3
  • 23
  • 26
  • REAL programmers use recursion when needed and they don't feel the need to brag about it. otherwise good solution ^^ – Jason Rogers Dec 03 '10 at 01:14