Incrementing C pointers

Sharky Forums


Results 1 to 2 of 2

Thread: Incrementing C pointers

Threaded View

  1. #1
    Goldfish qwe's Avatar
    Join Date
    Sep 2002
    Location
    London
    Posts
    69

    Incrementing C pointers - SOLVED

    I am playing with some C programming and I wrote this simple program to print the arguments that were fed to the program.
    Code:
    #include <stdio.h>
    
    int main(int argc, char * argv[]){
       char *pi;
       int i;
       pi = argv[1];
       printf("%d args.\n",argc-1);
       printf("input: ");
       for(i=0;i<argc-1;i++){
          printf("%c, ",*pi);
          /*The line below increments pi by 1 char worth of bytes */
          pi+=sizeof(pi)/2;
          /* An alternative to the above line is putting pi++ twice - why? */
       }
       printf("\n");
       return 0;
    }
    My question is that if I use the line pi++ shouldn't pi now point to the next char in the array? When I tried this it didn't work. I found that instead I had to increment pi twice for it to accurately put out the arguments as they were fed in.
    Last edited by qwe; 11-24-2008 at 07:41 PM. Reason: adding solution
    Possesions never meant anything to me,
    I'm not crazy.
    Well that's not true I have a bed,
    and a guitar and a dog named dog.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •