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

Recommended Answers

All 4 Replies

Create your function like this

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

and call it by passing the address of i

increment( &i );

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..

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.