String Reverse Without Declaring a Local Variable or calling another function?

I suppose that going straight to assembly language and using a couple of registers would be a good way not to land the job either.

Well, any solution to the original question is going to be a bad coding practice. Mine’s a whole lot better than, say, the recursive solution.

-lv

It could be they were looking for any “outside the box solution”, but probably they phrased the question more specifically than it was related to me. I think the answer they wanted was really the recursive solution - it doesn’t break any rules (true, I could run out of stack space but hypothetically I could run out of anything) and is completely self-contained. I think inlining assembler and accessing registers would escape them and they’d have to give credit where due…of course if they chose to run your program on a different processor than you’d inlined for it wouldn’t work. I’m not sure inlining is really “C” (I am not saying, more asking).

It isn’t, at least not in that sense (inlining assembler). It’s a common extension, but the Standard is silent on the issue (I think) and it certainly isn’t a way to write portable code.

The `inline’ keyword in relation to inlining functions (that is, giving the compiler a hint that this function ought to be inlined) was introduced in C99 and was, again, a common extension before that. (The fact that good compilers ignore such hints can be glossed over here.)

My 1978 copy of Kernighan & Ritchie includes just this on asm:

Harbison & Steele (1987) includes this:

For those who enjoy pointless programming problems:

Several years ago I had a programming job for the summer. I wasn’t used to spending that many hours a day staring at code, and it must have been affecting me somewhat, because I started having weird programming dreams. In one, I was trying to figure out a way to sort an array of unsigned integers into ascending order. But here’s the catch: I wasn’t allowed to use <, >, or ==, or to include elements of the array in the argument list of any function (not even indirectly, such as setting a variable equal to an expression that contains some of the numbers, and then passing it to a function.)

Don’t ask me why anyone would want to do such a stupid thing – like I said, it was a dream. The really funny part is that I actually figured out a way to do it while in my dream, and it actually works.

I’ll wait a little while before posting it, in case anyone wants to figure it out for themselves.

Squink: Since the first C standard was created in 1989, a book from 1978 (or 1987) could not be speaking of Standard C.

Plus, I hope you have more recent C references.

K&R was sort of the standard in the days before standards. (Yeah, I KNOW :smack: )
Oh yes, I have newer, but I can’t bring myself to toss such oldies but goodies.

It’s not that hard to do. For those who want a little hint, consider this: What is the value of (a + b + |a - b|)/2?

To get (a < b), you can do ((a - b) & 0x80000000) (assuming 32 bit integers).

I dunno if that goes against the spirit of your requirements, though, since it’s basically the same thing.