dazedandconfused69
asked on
small program will not compile in C (on linux server)
I am brand new to C and pointers and am working through some tutorials and going back over lectures to improve my foundation/survive my course. This is NOT homework. I just cannot figure out why I am getting this compile error on this program (which I have copied verbatim from my lecture notes).
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i;
int* iptr = &i;
for (iptr* = 0; *iptr < 10; (*iptr)++)
printf("%d\n",*iptr);
return(EXIT_SUCCESS);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Argh - now I see the typo - sorry - this was a dumb question.
ASKER
Thanks for answering this very dumb question.
Not dumb question - actually mistakes like this are common. I make them a lot. Sometimes, I can't see what is wrong, and I just write the section over again from scratch and it works. Then I look at my commented out version and have a good laugh. A second set of eyes is often useful.
>> I would hope that a good optimizing compiler would see that
For types like int, yes. However, for user-defined types that is generally not the case.
The reason to always prefer pre-increment over post-increment (even though you're sure it won't matter for the specific type you're working with), is to teach yourself a good habit.
For types like int, yes. However, for user-defined types that is generally not the case.
The reason to always prefer pre-increment over post-increment (even though you're sure it won't matter for the specific type you're working with), is to teach yourself a good habit.
(*iptr)++
should be written as
++(*iptr)
since the latter is more efficient (since a temporary copy need not be made).
I would hope that a good optimizing compiler would see that
(*iptr)++
in isolation, would be equivalent to
++(*iptr)
and optimize this for you.