Thread: Return struct

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Return struct

    How can i return a structure, I have tried this:
    Code:
    #include <stdio.h>
    
    struct list 
    {
    	int one, two=0;
    };
    typedef struct list item;
    
    int main()
    {
    	item thingM;	
    	thingM = funct();
    	printf("%d\n", thingM.one);
    }
    
    item funct()
    {
    	item thing;
    	thing.one = 10;
    	return thing;
    }
    But i get the error message:
    Code:
    new.c:5: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
    new.c: In function ‘main’:
    new.c:12: error: incompatible types in assignment
    new.c: At top level:
    new.c:16: error: conflicting types for ‘funct’
    new.c:12: error: previous implicit declaration of ‘funct’ was here
    Last edited by taurus; 10-02-2009 at 10:16 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You need a prototype for funct(), or you need to define it before main(). IE:
    Code:
    #include <stdio.h>
    
    
    struct list
    {
    	int one, two;
    };
    typedef struct list item;
    
    item funct();
    
    int main()
    {
    	item thingM;
    	thingM = funct();
    	printf("%d\n", thingM.one);
    }
    
    item funct()
    {
    	item thing;
    	thing.one = 10;
    	return thing;
    }
    This worked for me.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Furthermore, you cannot assign values inside the struct declaration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM