How to transfer value of a variable between functions

This is a discussion on How to transfer value of a variable between functions within the C Programming forums, part of the General Programming Boards category; Hey, Thie may seem like a stupid question, and you may tell me to go learn about pointers but can ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    How to transfer value of a variable between functions

    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.

  2. #2
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    You need to use the return statement. Heres a simple example, otherwise check the FAQ.

    Code:
    int main()
    {
         int i, j;
         i = 10;
         j = function1(i);
         return 0;
    }
    
    int function1(int i)
    {
         int b;
         b = i 
         return b;
    }
    Now j is 10.
    Last edited by ventolin; 05-18-2004 at 07:10 PM.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3
    Thanks ventolin, is there any way to do this with pointers?

  4. #4
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    is this what you mean?

    Code:
    int function1(int i);
    
    int main()
    {
         
        int i = 10;
        int *ptr;
    
        ptr = &i;
    
        i = function1(i);
    
        return 0;
    }
    
    int function1(int i)
    {
         i++;
         return i;
    }
    Last edited by ventolin; 05-18-2004 at 07:43 PM.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    3
    I guess, but this looks like C++, I think I figured out how Im gonna do it, thanks for the feedback.

  6. #6
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    oops my bad, i edited post

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by pdkx7
    Thanks ventolin, is there any way to do this with pointers?
    There's really no need to do it with pointers if all you need is the value. If, on the other hand, you want changes made to the passed object to be reflected in the original object, you need to use pointers to simulate passing by reference:
    Code:
    #include <stdio.h>
    
    void foo(int *p);
    
    int
    main(void)
    {
      int i = 0;
    
      printf("In main: %d\n", i);
      foo(&i);
      printf("In main: %d\n", i);
    
      return 0;
    }
    
    void
    foo(
      int *p
      )
    {
      printf("In foo: %d\n", ++(*p));
    }
    My best code is written with the delete key.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Prelude
    There's really no need to do it with pointers if all you need is the value. If, on the other hand, you want changes made to the passed object to be reflected in the original object, you need to use pointers to simulate passing by reference:
    i admire your intelligence

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  4. Variable Argument Functions
    By genghis in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2002, 01:01 PM
  5. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 10:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21