Thread: Understanding increment operator with pointers in C

  1. #1
    Registered User
    Join Date
    Mar 2021
    Posts
    2

    Question Understanding increment operator with pointers in C

    Hello Cprogramming Community, I need some clarification about this particular code below:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    
    int main(void)
    {
        char phrase[80],*p;
        int i,s=0;
        gets(phrase);
        p=phrase;
    
        while (*p!='\0')
            if (*(p++)==' ') s++;
        printf("Phrase has %d spaces.\n",s);
        
        return 0;
    }
    My question is, why p++ will get incremented after the if statement?
    Shouldn't it be the opposite?
    I really would like some explanation on that behavior here!

    Thanks in advance for your time!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,290
    You might want to revise your learning material on pre-increment versus post-increment; you may have mixed them up hence the confusion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    800
    Please take a look at the following to see the difference between pre and post increment operators:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int x = 10;
       int y = 0;
    
       // Pre-increment
       printf("%s\n", "Increment x before assigning the value to y");
       printf("Original x == %d\n", x);
       y = ++x;
       printf("x == %d\n", x);
       printf("y == %d\n\n", y);
    
       x = 10;
       // Post-increment
       printf("%s\n", "Increment x after assigning the original value to y");
       printf("Original x == %d\n", x);
       y = x++;
       printf("x == %d\n", x);
       printf("y == %d\n", y);
    
       return 0;
    }
    And the result:
    Code:
    $./foo
    Increment x before assigning the value to y
    Original x == 10
    x == 11
    y == 11
    
    Increment x after assigning the original value to y
    Original x == 10
    x == 11
    y == 10
    Also, NEVER use gets()! gets() has been depreciated in C99 and removed completely from the C11 C Standard. Please use fgets() instead!
    Last edited by rstanley; 4 Weeks Ago at 06:31 AM.

  4. #4
    Registered User
    Join Date
    Mar 2021
    Posts
    2
    Thanks for the answer, I was confused a bit with unary operators when I posted this and the only reason I used gets() was because the book I was reading had this as a solution to an exercise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. increment operator
    By Rohit88 in forum C Programming
    Replies: 5
    Last Post: 07-29-2013, 01:39 PM
  2. Problem understanding simple increment operation
    By Avenger625 in forum C Programming
    Replies: 4
    Last Post: 01-27-2013, 02:04 PM
  3. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 11:06 PM
  4. Help with increment operator, please
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-18-2008, 07:06 PM
  5. Replies: 11
    Last Post: 08-30-2004, 03:56 PM

Tags for this Thread