Sunday, 11 August 2013

cannot convert from 'int *' to 'int'

cannot convert from 'int *' to 'int'

So I'm looking at a code that's supposed to be an example of pass by
reference. This example comes from here:
http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr233.htm
When I compile it the error I get here is related to the "int temp=i" line:
Error 1 error C2440: 'initializing' : cannot convert from 'int *' to 'int'
And the other error relates to the "j = temp" line:
Error 2 error C2440: '=' : cannot convert from 'int' to 'int *'
I'm guessing it has something to do with pointers. I'm expecting to get
flamed for not having a greater knowledge of pointers here as I'm sure
it's a simple solution, but please bear in mind that I'm looking at this
code precisely for that reason!
Any help to get this working would be much appreciated.
Also I'm using C
`#include
void swapnum(int *i, int *j) {
int temp = i;
i = j;
j = temp;
}
int main(void) {
int a = 10;
int b = 20;
swapnum(&a, &b);
printf("A is %d and B is %d\n", a, b);
return 0; }`

No comments:

Post a Comment