Thread: incrementing a pointer

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    incrementing a pointer

    Code:
    void printCharacters( const char *ptStr ){
       for( ; *ptStr!='\0'; ++ptStr) {
            printf("%c",*ptStr);
    }
    }
    In this function ptStr points to a char, and char is 1byte, so ++ptStr means "point to the next byte"? If the array is of type int, then how to increment the pointer when looping through elemets? ++ptStr or ptStr+=4 (because int is 4byte)???
    Last edited by lmanukyan; 01-20-2016 at 08:45 AM. Reason: missed a curly bracket

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you increment a pointer, it automatically advances the necessary number of bytes to point to the next element of the given type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incrementing
    By erdemtuna in forum C Programming
    Replies: 3
    Last Post: 01-12-2016, 02:10 AM
  2. Pointer Incrementing
    By zoowho in forum C Programming
    Replies: 4
    Last Post: 02-25-2010, 12:42 PM
  3. Incrementing Twice?
    By Kemaratu in forum C Programming
    Replies: 3
    Last Post: 09-27-2009, 11:13 PM
  4. incrementing a char pointer
    By Bleech in forum C Programming
    Replies: 13
    Last Post: 11-20-2006, 11:25 PM
  5. Incrementing by 2 not 1
    By Eckey in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2004, 03:16 PM