0

I have declared a global variable. But when i assign the "i" in the loop as the function parameter, the global variable does not chang. It seems that it creats another variable "i" and assigns it to that "i". Is it possible once i have declared the global "i"?? I'm little bit confused.Can u plz explain it n hlp me out.:):?:

#include <stdio.h>
int i;
void increment( int i )
{
   i++;
}
int main()
{
   for( i = 0; i < 10; increment( i ) )
   {
   }
   printf("i=%d ", i);
   return 0;
}

thnx in adv...:-O

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by baby_c
0

Create your function like this

void increment( int *i )
{
   ++(*i);
}

and call it by passing the address of i

increment( &i );

Edited by gerard4143: n/a

0

Create your function like this

void increment( int *i )
{
   ++(*i);
}

and call it by passing the address of i

increment( &i );

no.i just need to explain whats going on..please help me for that..thanks for your reply..

0

Because you passing a copy of i to your function it will never increment the global i.

Edited by gerard4143: n/a

0

Because you passing a copy of i to your function it will never increment the global i.

yeah...thats true..but how could that happen...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.