++pointer works, pointer++ doesn't (C)

I'm counting the number of s's added to the ends of words. If a number is 1, I don't add an s, otherwise I do add one. This is C code (not C++; I have to use C for school) and it works.

The weird thing is, on line 47 if I change "++*ssAdded;" to "*ssAdded++;", it no longer works and the count always ends up at 0. It also works if I do "*ssAdded += 1;" and "*ssAdded = *ssAdded + 1;". Why doesn't it work if I write it as "*ssAdded++;"? It seems like that shouldn't make a difference but it does.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
#define MINUTE            60
#define HOUR   ( MINUTE * 60 )
#define DAY    ( HOUR   * 24 )

void formatSeconds( int *,int *,int *,int * );
void addS( int,int * );

int main()
{
  int seconds = 0,minutes = 0,hours = 0,days = 0,ssAdded = 0;

  printf( "Total Seconds: " );
  scanf_s( "%d",&seconds );
  getchar();

  formatSeconds( &seconds,&minutes,&hours,&days );
  printf( "Formatted: %d day",days );
  addS( days,&ssAdded );
  printf( ", %d hour",hours );
  addS( hours,&ssAdded );
  printf( ", %d minute",minutes );
  addS( minutes,&ssAdded );
  printf( ", %d second",seconds );
  addS( seconds,&ssAdded );
  printf( " (%d s's added)",ssAdded );
  getchar();

  return 0;
}

void formatSeconds( int *seconds,int *minutes,int *hours,int *days )
{
  *days    = *seconds /    DAY;
  *seconds = *seconds %    DAY;
  *hours   = *seconds /   HOUR;
  *seconds = *seconds %   HOUR;
  *minutes = *seconds / MINUTE;
  *seconds = *seconds % MINUTE;
}

void addS( int a,int *ssAdded )
{
  if( a != 1 )
  {
    printf( "s" );
    ++*ssAdded;
  }
}
The order of operations is completely different. In one form, you increment the value referenced by the pointer. In the other form, you increment the pointer and dereference the old value of the pointer.
Topic archived. No new replies allowed.