A simple pointers question

1
2
3
4
5
6
7
8
9
10
11
12
void MyFunc(int *x);
main()
{
   int z=5;
   MyFunc(&z);
   cout<<z;
}

void MyFunc(int *x)
{
   *x = (*x)++;
}


Does it work error-free? (cannot execute to verify it as have no C++ installed)
Will 'z' contain value 6 after main function finishes execution?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void MyFunc(int *x);
int main()
{
   int z=5;
   MyFunc(&z);
   std::cout<<z;
   return 0;
}

void MyFunc(int *x)
{
   *x = (*x)+1;
}


I've added to the function +1 instead of ++, because MinGW and VS have difference otherwise.
So my case shows 6 in both cases(VS and MinGW)

Or it can have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void MyFunc(int *x);
int main()
{
   int z=5;
   MyFunc(&z);
   std::cout<<z;
   return 0;
}

void MyFunc(int *x)
{
   *x = ++(*x);
}


The result is 6 of course on both compilers
Last edited on
Thx a lot!
Topic archived. No new replies allowed.