Before: 2293572, 2293572
After: 2293576, 2293573
Press any key to continue . . .
It is reported in one of the books that the program should not compile as the
arithmatic on k++ pointer is not permitted.unless the void pointer is
appropriately typecasted.
Can anyone explain whether the problem is from the Cev-Cpp++ software?
Thanks
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Since Dev-C++ is merely an IDE not a compiler, the problem will not be with
Dev-C++. However Dev-C++ is packaged with MinGW/GCC and that is what we need
to know. What version of Dev-C++ (and more importantly GCC) are you using? As
mentioned prominently in the READ BEFORE POSTING thread, you should always
post the complete text from teh "Compile Log" tab. Also use code tags when
posting code and logs.
In VC++ 2008 using C compilation that code yields:
main.c(11) : error C2036: 'void *' : unknown size
You cannot increment a void pointer because the compiler does not know the
size of the object it points to (despite what Drmoo may say). In your results
it appears to have implicitly assumed that it is a char*, so incremented the
address by one. However since that is incorrect behaviour, I either suspect
your results or possibly your compiler version; although I'd be surprised if
even GCC 2.95 behaved like that.
You should in any case add the compiler options
-Wall -Werror -Wformat
to your build. And you should use the format specifier %p to print pointers
not %u.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Note also that it is only allowed when C compilation is used, it is not
supported as an extension in C++ code. It is not valid ISO C and I suggest
that you do not use it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am using the following program.
int main()
{
int a=10, j;
void k;
j=k=&a;
printf("Before: %u, %u",j, k);
j++;
k++;
printf("\nAfter: %u, %u",j, k);
printf("\n");
system("PAUSE");
return 0;
}
The result of which is as follows
Before: 2293572, 2293572
After: 2293576, 2293573
Press any key to continue . . .
It is reported in one of the books that the program should not compile as the
arithmatic on k++ pointer is not permitted.unless the void pointer is
appropriately typecasted.
Can anyone explain whether the problem is from the Cev-Cpp++ software?
Thanks
In C++ you cannot implicitly convert from void* to anything else. In C,
however, this is perfectly legal.
In both languages incrementing pointers is 100% valid.
Since Dev-C++ is merely an IDE not a compiler, the problem will not be with
Dev-C++. However Dev-C++ is packaged with MinGW/GCC and that is what we need
to know. What version of Dev-C++ (and more importantly GCC) are you using? As
mentioned prominently in the READ BEFORE POSTING thread, you should always
post the complete text from teh "Compile Log" tab. Also use code tags when
posting code and logs.
In VC++ 2008 using C compilation that code yields:
You cannot increment a void pointer because the compiler does not know the
size of the object it points to (despite what Drmoo may say). In your results
it appears to have implicitly assumed that it is a char*, so incremented the
address by one. However since that is incorrect behaviour, I either suspect
your results or possibly your compiler version; although I'd be surprised if
even GCC 2.95 behaved like that.
You should in any case add the compiler options
to your build. And you should use the format specifier %p to print pointers
not %u.
Clifford
OK I just checked the manual, and incrementing a void pointer is a GNU
extension; [http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Pointer-Arith.html
Pointer-Arith](http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Pointer-Arith.html
%23Pointer-Arith).
Note also that it is only allowed when C compilation is used, it is not
supported as an extension in C++ code. It is not valid ISO C and I suggest
that you do not use it.