Like Tree2Likes
  • 2 Post By laserlight

Returning struct in function

This is a discussion on Returning struct in function within the C Programming forums, part of the General Programming Boards category; Hi, while I was reading the struct tutorial over cprogramming.com, I found this You can also return structures from functions ...

  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
    21,717
    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;
    }
    xeon321 and zaksd like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21