Thread: Pointer confusion

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Angry Pointer confusion

    Hello everyone,

    here is a sample code where myptr is passed to myfuction. It is said that it will not work. to make it working we shud pass &myptr. I have confusion here that pointer contains the address of the variable.(here an int). so when i pass this i am passing the address to the called function. so why cant i modify in the called function. what will be the difference if i pass &myptr??????

    Code:
    myfunction(int *ptr)
    {
    int myvar = 100;
    ptr = &myvar;
    }
    main()
    {
    int *myptr;
    myfunction(myptr);
    //Use pointer myptr.
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can not change a variable passed into a function (in this case ptr), since it is a local copy of the value passed in. To modify anything outside of the function, you would need the address of what you want to modify (which means that it needs to be passed in as a pointer). So to modify your current pointer, you need to pass a pointer to pointer (so take the address of myPtr.

    That is not the end of your problems, however, since myVar is a local variable in myfunction - the memory used by myVar is freed when the code returns. So you are now pointing at memory that is not in use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1st, to change anything you need to pass by reference/pointer. This applies to pointers themselves as well. If you want to change the value of a pointer - where it points to (as opposed to what it points to) - then you need to pass the address of the pointer such that the called function can change it's value. So, your function would need to accept a pointer-to-pointer-to-int instead of a pointer-to-int, and you'd need to pass the address of that pointer to the called function.

    2nd, you are trying to point to a local variable which will get popped off the stack the second the function ends. Even if you fix problem #1, your "returned" pointer is still going to be pointing to an area on the stack that it should probably avoid.

    [edit]Drats... beaten![/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Good Explanation Thanks to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM