Results 1 to 9 of 9

Thread: Function Returning Pointer Adress

  1. #1
    Join Date
    Oct 2009
    Beans
    13

    Thumbs down Function Returning Pointer Adress

    Hey guys I'm trying to make a function that returns a pointer address. Do you mind sharing how I would declare the prototype and how I would assign the pointer when I call it? Thank you

  2. #2
    Join Date
    Jan 2009
    Location
    Arizona, US
    Beans
    113
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Function Returning Pointer Adress

    Assuming you are asking about C++, it's simple.

    Code:
    int& getIntPtrAddress()
    {
        int someNumber = 10;
        int& address = someNumber;
    
        return address;
    }

  3. #3
    Join Date
    Jul 2008
    Beans
    1,706

    Re: Function Returning Pointer Adress

    Prototype:
    Code:
    //example 1.  This returns the actual address
    int *someFunctionA(int arg);
    
    //example 2.  This takes a pointer as well as an arg and stores the data in the pointer's location
    void someFunctionB(int arg, int *dest);
    Calling the function:

    Code:
    int *foo;
    foo = someFunctionA(5); //Example 1
    someFunctionB(5, foo); //Example 2

  4. #4
    Join Date
    Oct 2009
    Beans
    13

    Re: Function Returning Pointer Adress

    Oh sorry I was just talking about C. So here is my program attached. It's not all done yet
    and I'm still learning so be prepared to find a lot of bugs haha
    Basicly I'm trying to write a function for a vending machine simulation program for my class
    and this function is how the user chooses the snack that they want. I just want it to return
    the address of the matricie(however you spell that) element chosen.
    Attached Files Attached Files
    Last edited by cmwarre; November 10th, 2009 at 03:58 AM. Reason: Forgot to attach file

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,222
    Distro
    Kubuntu

    Re: Function Returning Pointer Adress

    Quote Originally Posted by efexD View Post
    Assuming you are asking about C++, it's simple.

    Code:
    int& getIntPtrAddress()
    {
        int someNumber = 10;
        int& address = someNumber;
    
        return address;
    }
    wtf?

    You are returning a reference to a variable that will 'die' as soon as the function returns. Where the heck did you learn to write code?

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,222
    Distro
    Kubuntu

    Re: Function Returning Pointer Adress

    Quote Originally Posted by cmwarre View Post
    Oh sorry I was just talking about C. So here is my program attached. It's not all done yet
    and I'm still learning so be prepared to find a lot of bugs haha
    Basicly I'm trying to write a function for a vending machine simulation program for my class
    and this function is how the user chooses the snack that they want. I just want it to return
    the address of the matricie(however you spell that) element chosen.
    This is not a forum for seeking assistance with homework! Consult with your teacher.

    However, just to critique your code...

    It appears that you have a 2-dim array of int values; why do you need to return a pointer?? Just return the value once you know the row and column of interest.

    Btw, wrt code, do you know what a "kitchen sink" refers to? It is when one plops many different steps into one function. Try to to simplify your code so that you are not querying the user for input in the same function where you are accessing the data from the 'matrix'. Also, look into toupper() (or tolower()).

    Lastly, insert code within CODE tags; attaching a zipped file is lame.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Beans
    2,185
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Function Returning Pointer Adress

    Quote Originally Posted by efexD View Post
    Assuming you are asking about C++, it's simple.

    Code:
    int& getIntPtrAddress()
    {
        int someNumber = 10;
        int& address = someNumber;
    
        return address;
    }
    This code is a recipe for disaster.

    The int someNumber is on the stack. As soon as the function returns, it disappears. The function returns a reference to a variable that has been cleaned up. This will lead to undefined behaviour. It might appear to work, but there's also a big chance that the program will crash.
    Ubuntu 12.04

  8. #8
    Join Date
    Apr 2009
    Location
    Toronto, Canada
    Beans
    93
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Function Returning Pointer Adress

    Quote Originally Posted by efexD View Post
    Assuming you are asking about C++, it's simple.

    Code:
    int& getIntPtrAddress()
    {
        int someNumber = 10;
        int& address = someNumber;
    
        return address;
    }
    The problem is that somenumber is declared and defined within a function. Therefore its scope is restricted to that function. As soon as the function ends, somenumber ceases to exist which means that the address that you've collected that is supposed to point to it will point to nothing (dangling pointer = dangerous, undefined, unwanted behaviour).
    You have two choices here: 1) declare somenumber in the context that will call getIntPtrAddress so that the int can be passed as an argument to the function and then the function may extract the address and send it back in a return statement; 2) declare someNumber dynamically within the function so that it will not disappear when the functions execution is finished:
    Code:
    int * bobby;       //create a pointer to an array
    bobby = new int;   //attribute a dynamic int to it 
    //I haven't tested this code so try it out and let me know if you have trouble with it.
    So your final code would look like this:
    Code:
    //solution 1
    void main()
    {
         int somenumer = 20;
         int& = getIntPtrAddress(somenumber);
    }
    int& getIntPtrAddress(anumber)
    {
        return &anumber;
    }
    
    //solution 2
    int& getIntPtrAddress()
    {
        int * somenumber;
        somenumber = new int;
        *somenumber = 10;
    
        return somenumber;
    }
    I've been away a while from c and its relatives so please don't hesitate to point out any erroneous information...
    Last edited by silentrebel; November 10th, 2009 at 04:12 PM.
    L'idée de communauté consiste d'offrande constante sans s'attendre à quoi que ce soit d'elle.
    The idea of community consists of constant giving without expectation of return.

  9. #9
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Function Returning Pointer Adress

    Closed since the thread is related to a homework question.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •