Thread: Functions that return structures

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Functions that return structures

    I've been trying to create a code that includes structures produced at various event times by a function. Since it was clear that my structure-producing function was not working, I decided to try building a very simple code that just produces a structure from a function.

    Not surprisingly, it doesn't work either. It returns an error saying "funs returning structs/unions illegal '=makepoint(1,1)' ".
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include <time.h>	
     #include <math.h>
    
    struct point {
    	int x;
    	int y;
    };
    struct point makepoint(int a, int b);	
    
    int main() {
    	struct point point1;
    	
    	point1=makepoint(1,1);
    	
    	printf( "done" );
    
    	getchar();
    	return 0;
    
    }
    
    struct point makepoint(int a, int b) {
    	struct point new;
    	
    	new.x=a;
    	new.y=b;
    	
    	return new;
    }
    I presume I'm making a really elementary coding error.

  2. #2
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    new is a local instance of point that'll be destroyed once the function returns. generally passing around and returning full structs creates alot of unnecessary overhead. instead, try passing a pointer to the struct.

    Code:
    #include <stdio.h>
     #include <stdlib.h>
     #include <time.h>	
     #include <math.h>
    
    struct point {
    	int x;
    	int y;
    };
    void makepoint(int a, int b, struct point *ppt);	
    
    int main() {
    	struct point point1;
    	
    	makepoint(1,1,&point1);
    	
    	printf( "done" );
    
    	getchar();
    	return 0;
    
    }
    
    void makepoint(int a, int b, struct point *ppt) {
            ppt->x=a;
            ppt->y=b;
    }
    Last edited by Bleech; 04-23-2008 at 09:12 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This is how to do it with dynamic memory.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>	
    #include <math.h>
    
    struct point {
    	int x;
    	int y;
    };
    
    struct point * makepoint(int a, int b);	
    
    int main() {
    	struct point * point1;  // declare a pointer 
    	
    	point1=makepoint( 1, 1 );  // a pointer to the struct is returned 
    	
    	printf( "done. x=&#37;d, y=%d\n", point1->x, point1->y  );
    
    	free(point1) ;   // release the struct's memory 
    	
    	getchar();
    	return 0;
    
    }
    
    struct point * makepoint(int a, int b) {
    	struct point * mypoint;  // Declare a pointer to a point struct 
    	mypoint = malloc(sizeof(struct point) ) ;  // Obtain dynamic memory  
    	
    	mypoint->x=a;  // assign values 
    	mypoint->y=b;
    	
    	return mypoint ;  // return the pointer to the struct 
    }
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Burra View Post
    It returns an error saying "funs returning structs/unions illegal '=makepoint(1,1)' ".
    What compiler are you using?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Quote Originally Posted by Dave_Sinkula View Post
    What compiler are you using?
    Good question. As it happens, I was using Miracle C, and when I switched to CodeBlocks, it worked (after I renamed "new").

    Thanks, Bleech and Todd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM