5

I have a beginners C question. I want in the code below...

include <stdio.h>

void iprint();
int i=0;

int main()
{
  int j;

  for (j=0; j<50; j++)
    {
      iprint(i);
      printf("%d\n",i);
    }
}

void iprint(i)
{
  i +=1;
  //printf("%d\n",i); 
}

... the function "iprint" to update the value of i each time is it called, e.g. update i so that it can be used in main with the value 1 for iteration 2, and 3 for iteration 2 etc.

I accomplished this by changing the code to this:

 include <stdio.h>

int iprint();
int i=0;

int main()
{
  int j;

  for (j=0; j<50; j++)
    {
      i= iprint(i);
      printf("%d\n",i);
    }
}

int iprint(i)
{
  i +=1;
  //printf("%d\n",i); 
  return(i);
}

Do i have to return(i) to make that happen? The reason for asking, is that if i have a lot of functions using i, it's a bit annoying having to pass i between them. If you instead, somehow could update i like you update a global variable in matlab, that'd be great. Is it possible?

4
  • It is possible, but not necessarily a good idea... Commented Sep 23, 2013 at 10:10
  • This is not even syntactically correct. Does this actually compile? (not to mention using global variables is generally a bad smell ) Commented Sep 23, 2013 at 10:11
  • Just remove the argument of iprint. Commented Sep 23, 2013 at 10:28
  • possible duplicate of Pointers in C and how to pass local variables as pointers Commented Sep 23, 2013 at 10:36

5 Answers 5

7

Use a pointer to point to the global variable. Change the pointer value. Thats it

Sign up to request clarification or add additional context in comments.

2 Comments

That's probably the best answer in my opinion since it assess the underlined question of how to modify a variable from outside the scope of a function.
Hey. Can u give us a example code ? :v In theory this is the solution, but i just did that and it did'nt work...
6

The problem with the first is that you pass the variable as an argument to the function, so when the function modifies the variable it's only modifying its own local copy and not the global variable. That is, the local variable i shadows the global variable i.

Not to mention that you don't actually declare the argument properly, so your program should not even compile.

4 Comments

And you could modify this behaviour by passing a pointer to i, e.g. void iprint( int *i ) { *i += 1 } and calling iprint( &i );.
@PP. Why pass a global variable to a function at all?
@JoachimPileborg Maybe to make the function pure? Or to make the dependency visible.
@JoachimPileborg If the purpose of the function is to modify a variable then that function could be made generic and re-usable by accepting, as an argument, the variable to be modified. It makes software engineers happy because it helps keep coupling low. However you are correct that one need not do this in this context.
2

You don't need to pass global variables as parameters. If you declare a parameter or local variable with the same name as the global variable you will hide the global variable.

include <stdio.h>

void iprint();
int i=0;

int main()
{
  int j;

  for (j=0; j<50; j++)
    {
      iprint();
      printf("%d\n",i);
    }
}

void iprint()
{
  i +=1;  /* No local variable i is defined, so i refers to the global variable.
  //printf("%d\n",i); 
}

Comments

0

you could have incremented the value of i in main function itself. By the way change the function to

int iprint(int i){
/*you have to mention the type of arguemnt and yes you have to return i, since i  
variable in this function is local vaiable when you increment this i the value of  
global variable i does not change.
*/
return i+1;
}

the statement

i=iprint(i); //this line updates the value of global i in main function


This happens like this because you are passing value in function by 'pass by value' method where a copy of variable is made. When you increment the i iprint method copy of global variable i is incremented. Global variable remains intact.

Comments

0

Must try this code

#include <stdio.h>
int i=0;
void iprint()
{
  i =i+1;
  //printf("%d\n",i); 
}
int main()
{
    int j;
    for (j=0; j<50; j++)
    {
      iprint();
      printf("%d\n",i);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.