expression question

This is a discussion on expression question within the C Programming forums, part of the General Programming Boards category; I have a question is *++p and ++*p legal expressions i have seen *++p used however i have not seen ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Question expression question

    I have a question is *++p and ++*p legal expressions i have seen *++p used however i have not seen ++*p put in to use

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, they're both legal. One alters the address of p and then dereferences (which is kind of useless by itself), and the other dereferences p and then alters the result.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    if i had
    char a[ ] = "cba", *p = a;
    then ++*P would b equal to c right??i am confused now

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the value thats stored in p (*p) would be the first element in the array, so it would be 'c'. if you increment this value (++*p) it will add one to 'c' so the result is 'd'

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Start slowly when going through it. First let's turn it into a program:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char a[ ] = "cba";
    	char *p = a;
    	
    	printf("*p = &#37;c\n",*p);
    	++*p;
    	printf("*p = %c\n",*p);
    	
    	return 0;
    }
    Output:

    Code:
    *p = c
    *p = d
    So why?

    Basically you have an array of chars that are like this in memory:

    Code:
    'c', 'b', 'a', '\0'
    You set p to point to this array. That means p holds the address of a, and can be treated in some ways as if it's a.

    Arrays are kind of special when you consider how the relationship they share with pointers. When you use an array name, you're usually referencing a pointer to the first element of the array. This is important. Pointing p to a actually points p at the first element of a.

    So what does ++*p do?

    First of all, it takes p, and dereferences it, and then performs the ++ on whatever the result was. This is equivalent of typing this out:

    Code:
    ++(*p);
    This means that you're grabbing where p points at and altering what it points at to be one more than it used to be.

    Since p points at the memory location of letter c inside the array, you end up ++ing the letter c. This causes c to be d, and the array a to look like this in memory:

    Code:
    'd', 'b', 'a', '\0'
    Hope this helps.

    Edit: Oh, well, beaten again. lol..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Tokenizing an expression, strtok or other alternatives?
    By _Marcel_ in forum C Programming
    Replies: 3
    Last Post: 03-04-2009, 07:57 AM
  3. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21