Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

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);

}

Open in new window

0
dazedandconfused69
Asked:
dazedandconfused69
  • 3
  • 2
1 Solution
 
phoffricCommented:
A quick look and I see one sure typo:
    for (iptr* = 0; *iptr < 10; (*iptr)++)
reverse the iptr and the * to be *iptr.
0
 
phoffricCommented:
BTW, some people claim that
    (*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.
0
 
dazedandconfused69Author Commented:
Argh - now I see the typo - sorry - this was a dumb question.  
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dazedandconfused69Author Commented:
Thanks for answering this very dumb question.
0
 
phoffricCommented:
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.
0
 
Infinity08Commented:
>> 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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now