Thread: Returning a pointer.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Returning a pointer.

    well, how can I return a pointer from a called function? I mean lets say I have a main function for printing a pointer, and have another function thats returning pointers(doesn't matter what the pointer is), how can I link between the main function and the another called function, also how can I generally make a function for returning pointers? if you can make an example(doesn't matter what it would be about..just having the same content) then would be much appreciated! ... thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Pointers are just variables, and are returned from functions just like any other variable would be.

    Rather than give you a complete example, I will give you a sample function:

    Code:
    char *make_string(void)
    {
        char *str = malloc(7);  /* error-checking omitted */
    
        strcpy(str,"Hello.");
    
        return str;
    }
    Your job is to create a program with a "main()" function that receives the pointer returned from this function, and print the result.

    Bonus points for doing what else is required with the pointer returned in this example.

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Matticus View Post
    Pointers are just variables, and are returned from functions just like any other variable would be.

    Rather than give you a complete example, I will give you a sample function:

    Code:
    char *make_string(void)
    {
        char *str = malloc(7);  /* error-checking omitted */
    
        strcpy(str,"Hello.");
    
        return str;
    }
    Your job is to create a program with a "main()" function that receives the pointer returned from this function, and print the result.

    Bonus points for doing what else is required with the pointer returned in this example.
    Alright will do no problem and thank you very much, also for putting the kind of your function "*make_string(void)" above is a "char" because the value of the pointer that're willing to print it is a char, so if the value of the pointer was resembling an integer number then I must change the kind to int aka "int *make_string(void)"...right?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The terminology you're using is a bit off, but basically yes - a pointer to character (char *) points to a character (char), and a pointer to integer (int *) points to an integer (int).

    Also, I posted that exercise in the hopes you would post your attempt so we can see how well you might understand it. I would like to see you try that.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by RyanC View Post
    Alright will do no problem and thank you very much, also for putting the kind of your function "*make_string(void)" above is a "char" because the value of the pointer that're willing to print it is a char, so if the value of the pointer was resembling an integer number then I must change the kind to int aka "int *make_string(void)"...right?
    To be more precise on the language:

    make_string is a function that returns a char * (aka a "pointer to char").

    A pointer can be thought of as a memory address. Here, we're saying this function returns a memory address, and we tell the compiler what type of data is stored at that address - in this case, a character (the "H" in "Hello.", specifically).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a pointer
    By Canadian0469 in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2008, 04:06 AM
  2. returning this pointer
    By chottachatri in forum C++ Programming
    Replies: 4
    Last Post: 03-30-2008, 08:35 AM
  3. Pointer returning too big value
    By emanresu in forum C Programming
    Replies: 7
    Last Post: 01-08-2007, 09:54 AM
  4. returning a pointer
    By starX in forum C Programming
    Replies: 18
    Last Post: 08-23-2002, 05:35 AM
  5. returning a pointer
    By uler in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 01:59 PM