Thread: Array or Pointer?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    46

    Array or Pointer?

    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
    C code. C code run. Run code, run...please!

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    the pointer points after the loop to the '\0'...

    so it's logical that puts() fails :-)

    i would use your second function, because it is easier to use and the speed is the same :-)
    Hope you don't mind my bad english, I'm Austrian!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    How can I get the first function to work?
    C code. C code run. Run code, run...please!

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can get the first function to work by using the notation of the second function. The reason why the first function fails is that you are replacing the message-pointer. When the while-loop has finished, this pointer points to the last character of your string. And since this is '\0', it goes wrong,

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    I understand now, thank you. Here is the fixed function, it works just fine now.
    Code:
    #ifdef DEBUG
      void encrypt(char *message)
      {
        char *p = message;
    
        assert(p != NULL);
        while(*p != '\0'){
          *p += (char)13;
          p++;
        }
        (void)puts(message);
    
        return;
      }
    #endif
    Either way I need to create a local variable, so I suppose either function would be okay. It's dependant more on style than any real improvements.
    C code. C code run. Run code, run...please!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  3. pointer to array of structs
    By Luken8r in forum C Programming
    Replies: 2
    Last Post: 01-08-2008, 02:05 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM