0

For some reason it prints 1 instead of 2 and i cant tell why.

#include <stdio.h>
#include <stdlib.h>
void f(int *p)
{
   *p++;

}
int main()
{
   int k=1;
   f(&k);
   printf("%d",k);
    return 0;
}
Michael
  • 82
  • 5

1 Answers1

1

Because you increment the pointer address and not it's value. Wrap it with parenthesis:

#include <stdio.h>
#include <stdlib.h>
void f(int *p)
{
   (*p)++;

}
int main()
{
   int k=1;
   f(&k);
   printf("%d",k);
    return 0;
}
ItayB
  • 10,377
  • 9
  • 50
  • 77