Thread: Does this function return structure pointer

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    20

    Does this function return structure pointer

    Does this function return structure pointer mypoint ?

    Code:
    #include <stdio.h>
    
    struct student
    {
        int x;
        int y;
    };
    
    
    // function prototype
    struct student* display(struct student *mypoint);
    
    
    int main()
    {
        struct student mypoint;
        mypoint.x = 1;
        mypoint.y = 2;
        
        display(&mypoint);   // passing struct as an argument
        
        return 0;
    }
    struct student* display(struct student *mypoint) 
    {
      printf("\n Displaying information\n");
      printf("\n x: %d", mypoint->x);
      printf("\n y: %d", mypoint->y);
      
      return mypoint;
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    671
    Yes, display() does return the pointer.

    You do not capture the return from display(). Either capture the return value, or better still, declare display() as void.
    Code:
    void display(struct student *mypoint) 
    {
      printf("\n Displaying information\n");
      printf("\n x: %d", mypoint->x);
      printf("\n y: %d", mypoint->y);
    
      // No return needed
    }
    You should also avoid the same name "mypoint" in main(), and in display(). Too confusing as they are different variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-04-2017, 07:42 AM
  2. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  3. Replies: 3
    Last Post: 03-14-2013, 03:25 PM
  4. Function to return pointer to structure
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 06-25-2002, 06:10 AM

Tags for this Thread