Thread: simple pointer arithmetic

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    simple pointer arithmetic

    I have a question regarding pointers. Let's say I pass a character array through a function parameter as a constant pointer as such:


    Code:
    void main(void)
    {
          char disk[10];
          // some code until
          usart_str_write(disk);
    
          // some more code
    }
    
    void usart_str_write(const char* string)
    {
         while (*string != "\0")
         {
               Usart_Write(*string);
               string++;
         }
    }
    given function declaration

    Code:
    char Usart_Write(char data);
    In usart_str_write, should I do anything to make the pointer *string derefence back to the initial character, although I won't be doing any more tasks with it?

    Thanks.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's perfectly fine to leave the pointer pointing to absolutely anything you like when exiting the function as the pointer goes out of scope and then will simply cease to exist.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. understanding pointer arithmetic
    By firstofthegreat in forum C Programming
    Replies: 3
    Last Post: 02-28-2008, 11:08 PM
  2. Simple pointer question
    By sweets in forum C++ Programming
    Replies: 9
    Last Post: 11-16-2004, 08:48 PM
  3. Pointer arithmetic question
    By moneil in forum C Programming
    Replies: 6
    Last Post: 03-25-2004, 11:45 PM
  4. Pointer arithmetic
    By freebu in forum C Programming
    Replies: 4
    Last Post: 04-27-2003, 09:27 AM