Thread: why this output?

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    9

    why this output?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
     int *a, *s, i;
      
     s = a = (int *) malloc(4 * sizeof(int));
     
     for(i = 0; i < 4; i++)
     {
      *(a + i) = i * 10; /* a[i] = i * 10 */
     }
     
     printf("*s++ = %d\n", *s++);       /* output: 0 */
     printf("(*s)++ = %d\n", (*s)++);   /* output: 10 */
     printf("*s = %d\n", *s);           /* output: 11 */
     printf("*++s = %d\n", *++s);       /* output: 20 */
     printf("++*s = %d\n", ++*s);       /* output: 21 */
     
     getchar();
     return 0;
    }
    I would like to know how it processed the output.....
    thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's meant to show you the difference between incrementing a pointer (s++) and incrementing what a pointer points to (*s)++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Quote Originally Posted by Salem
    It's meant to show you the difference between incrementing a pointer (s++) and incrementing what a pointer points to (*s)++
    yeah, but how is it worked out ?

    for example,

    printf("*s++ = %d\n", *s++);

    (*s)++ it increments the pointer, but i can't really see how the output is 0
    Last edited by dredre; 05-08-2004 at 03:31 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dredre
    yeah, but how is it worked out ?
    for example,

    printf("*s++ = %d\n", *s++);

    (*s)++ it increments the pointer, but i can't really see how the output is 0
    That's because you've forgotten basic multiplication. Anything multiplied by zero is zero:
    Code:
    *(a + i) = i * 10; /* a[i] = i * 10 */
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well why don't you print out the whole array (there's only 4 numbers) between each of the print statements you have, so you can see how it changes as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM