Here's a funny problem. I want to take a string and encrypt it, which works fine when I treat the string that is passed to my function as an array, but when I treat it as a pointer it encrypts the character that it is pointing to and then appears to delete it from the array as it moves on to the next character.
I've used this notation a number of times and I'm pretty sure I've got it right, but this behavior is something I haven't ever seen before. Here's the code, the production function works great but the debug function flops out and prints nothing but a newline.
Code:#include <stdio.h> #include <assert.h> #define DEBUG /*#define PRODUCTION*/ static void encrypt(char *message); int main(void) { char message[] = "A silly message to encrypt"; encrypt(message); return 0; } #ifdef DEBUG void encrypt(char *message) { assert(message != NULL); while(*message != '\0'){ *message += (char)13; message++; } (void)puts(message); return; } #endif #ifdef PRODUCTION void encrypt(char *message) { int i = 0; assert(message != NULL); while(message[i] != '\0'){ message[i++] += (char)13; } (void)puts(message); return; } #endif