2 Quick Pointer Questions:
I have two questions about pointers:
Code:
#include <iostream.h>
void somefunc(int * fptr)
{
*fptr += 250;
}
void somefunc2(int * fptr2)
{
*fptr2 = 450;
}
void somefunc3(int * fptr3)
{
*fptr3 ++;// This doesn't work.
}
main()
{
int x = 3;
int * Xaddr = &x;
cout << x << endl;
somefunc(Xaddr);
cout << x << endl;
somefunc2(Xaddr);
cout << x << endl;
somefunc3(Xaddr);
cout << x << endl;
return 0;
}
Yet, strangely, the same line here does:
Code:
#include <iostream.h>
void somefunc(int * fptr)
{
*fptr += 250;
}
void somefunc2(int * fptr2)
{
*fptr2 = 450;
}
void somefunc3(int * fptr3)
{
*fptr3 += 1; // This does work.
}
main()
{
int x = 3;
int * Xaddr = &x;
cout << x << endl;
somefunc(Xaddr);
cout << x << endl;
somefunc2(Xaddr);
cout << x << endl;
somefunc3(Xaddr);
cout << x << endl;
return 0;
}
My two questions are thus:
1.) In cases like this, does the ++ operator not work?
2.) Is this one of the primary uses of pointers? (Through functions)
Re: 2 Quick Pointer Questions:
Quote:
Originally posted by Krak
I have two questions about pointers:
[code]
void somefunc3(int * fptr3)
{
*fptr3 ++;// This doesn't work.
}
void somefunc3(int * fptr3)
{
*fptr3 += 1; // This does work.
}
My two questions are thus:
1.) In cases like this, does the ++ operator not work?
2.) Is this one of the primary uses of pointers? (Through functions)
This is the error in first form
*fptr3++; gets translated this way
1) *fptr;
2) fptr++;
I bet that's now what you wanted
So the correction is (*fptr)++;
(*fptr)++ is as good as
(*fptr) + = 1; or (*fptr) = (*ftpr) + 1;