Thread: Passing Pointer to a function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Passing Pointer to a function

    I am not getting the obvious output (67, 67) for this program. Why I don't get the value of the local variable of a fucntion to which i am passing a pointer. I can pass the value of a variable to a function through pointer , but not receive the value from a function through a pointer.


    #include "stdio.h"

    void func(int *p)
    {
    int i= 67;
    p = &i;
    printf("\nIn func() %d\n",*p);
    }

    void main(void)
    {
    int *ptr;
    func(ptr);
    printf("\nIn main() %d\n",*ptr);
    }



    Thanks,
    Nishant Ghai

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: Passing Pointer to a function

    Originally posted by Nishant Ghai
    I am not getting the obvious output (67, 67) for this program. Why I don't get the value of the local variable of a fucntion to which i am passing a pointer. I can pass the value of a variable to a function through pointer , but not receive the value from a function through a pointer.


    #include "stdio.h"

    void func(int *p)
    {
    int i= 67;
    p = &i;
    printf("\nIn func() %d\n",*p);
    }

    void main(void)
    {
    int *ptr;
    func(ptr);
    printf("\nIn main() %d\n",*ptr);
    }



    Thanks,
    Nishant Ghai
    First, don't void your mains, ... int them
    main should return an int.
    Second, scope of the variable i, is limited to the function fun, outside of that it has no meaning neither has its address.
    Third,ptr is a pointer, and is passed by value, its value in main is not changed just by passing it in a function, you can possibly change the value of what a pointer points to this way but not the pointer itself.
    Last edited by pinko_liberal; 02-27-2003 at 12:00 AM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    you could probably fix all your problems by making *ptr outside of the main function as a global variable, but that's just the lazy way . With any program bigger than that one it would of course be better to restructure your functions to fix the problem

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Well when func ends, i goes out of scope, so ptr isn't valid anymore.
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you really want to do this...
    Code:
    #include "stdio.h"
    
    void func(int **p)
    {
      static int i= 67;
      *p = &i;
      printf("\nIn func() %d\n",**p);
    }
    
    int main(void)
    {
      int *ptr;
      func(&ptr);
      printf("\nIn main() %d\n",*ptr);
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM