Thread: Returning struct in function

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    Question Returning struct in function

    Hi, while I was reading the struct tutorial over cprogramming.com, I found this

    You can also return structures from functions by defining their return type as a structure type. For instance:

    struct database fn();


    "Return structures from functions by defining their return type as a structure type"

    I tested it by writing a simple program. I am wondering what should I type after return. I tested each of the followings. But doesn't seem to work. Mind to give some help?

    Code:
    return data;
    return struct data;
    return data.value;
    My code
    Code:
    
    
    Code:
    #include <stdio.h>
    
    
    struct data get_value(void);
    
    
    struct data
    {
    	int value;
    	float sales;
    };
    
    
    
    
    int main(void)
    {
    
    
    	getchar();
    	return 0;
    }
    
    
    struct data get_value(void)
    {
    	printf("Enter value : ");
    	scanf("%d", &data.value);
    
    
    	printf("\nEnter sales : ");
    	scanf("%f", &data.sales);
    
    
    	return data.value;  // <--- This one
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not create an object of type struct data, so there was nothing to store into. Contrast with:
    Code:
    #include <stdio.h>
    
    struct data get_value(void);
    
    struct data
    {
        int value;
        float sales;
    };
    
    int main(void)
    {
        struct data data_object = get_value();
        getchar();
        return 0;
    }
    
    struct data get_value(void)
    {
        struct data data_object;
    
        printf("Enter value : ");
        scanf("%d", &data_object.value);
    
        printf("\nEnter sales : ");
        scanf("%f", &data_object.sales);
    
        return data_object;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Oh! Now I see. Thanks

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    I was wondering if I can explain the line
    Code:
    structdata data_object = get_value();
    Like this,
    *By using the terms data type, variable name and value*

    Data type Variable name Value
    __________ __________ __________
    |struct data| |data_object| |get_value();|
    ========= ========= ==========


    Last edited by xeon321; 06-24-2012 at 01:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. Returning a pointer to a struct
    By iain in forum C Programming
    Replies: 1
    Last Post: 03-23-2005, 04:42 PM
  4. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM
  5. problem returning struct member from function
    By yukster in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 01:21 PM