Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to transfer value of a variable between functions

Status
Not open for further replies.

pdkx7

Programmer
May 18, 2004
1
US
Hey,

Thie may seem like a stupid question, and you may tell me to go learn about pointers but can you guys try and help me out here.

I want to transfer the value of a variable from one function to another for example:

int main()
{
int i;
i = 10;
}

int function1()
{
int b;
b = i
//I want 'b' to have the value of 'i'which is 10, how would i do this with using pointers or is there another way around?
}
Thanks for any feedback.
 
Do you know anything about function parameters?
Code:
int func(int x)
{
  int b = x + 1; /* use argument value via parameter */
  ...
  return b;
}
...
int main()
{
  int a, w;
  a = .....
  ...
  w = func(a); /* pass a value as argument */
  ....
}
If you want to change argument value in the function:
Code:
void func(int* px)
{
  int b = *px; /* get current value if you need */
  ....
  *px = b; /* change argument value */
}
...
int main()
{
  int a = 1;
  ...
  func(&a);
  /* Now a has a new value calculated by func() */
}
It's a basic C mechanics, you must study it before writing any C programs...
Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top