Just a Question...

I have a simple question. Can a pointer be incremented in a for loop? In my program, I use a pointer variable and use it in a for loop like such:
for(*var1 = 1; *var1<100 ;*var1++)
int function(int* var1)

{
if(*var1 blah blah)
{do blah blah}
if(blah)
{blah}
}
}
The variable is not incremented by the ++ even when the if conditions are not met and the loop shouldn't break. the *var1 stays at 1. Any help would be appreciated. Thank you!

that should be the calculation for monthly payment how would I code that?

Yes of course. This is the classic example of copying character strings from Kernighan and Ritchie:

1
2
3
void strcpy(char* src, char* dst) {
    while (*(dst++) = *(src++));
}

Funny old snippet, using the fact that string is terminated with zero character which makes the loop stop when copied :)

You only need to decide, what are you incrementing - the pointer itself or the value to which it points. Use parentheses, perhaps.
Last edited on
*p++ means *(p++). That is, p gets incremented and the expression evaluates to what p pointed to previously. If you want to increment what p points to, you need (*p)++. Alternatively, you can use ++*p.
Thank you so much, both of you( ! x ∞ ) The method that worked was: (*var1)++
Topic archived. No new replies allowed.