Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Returning structure reference in a C function
Returning structure reference in a C function [message #759319] Mon, 28 November 2011 11:19 Go to next message
Shashank  is currently offline Shashank Friend
Messages: 6
Registered: January 2011
Junior Member
Hello,

I was wondering if someone could let me know if there are any issues with the CDT and returning structure references.

I have the below code for reading file content into a linked list:



struct list_node
{
  char dir[BUFSIZ];
  struct list_node *next;
};

typedef struct list_node list;

int main( )
{
	parse_file();
}


//list is structure

/*Error line*/ list * parse_file()

{

	const char filename[] = "find_inotify.txt";
	FILE *file = fopen(filename, "r");
	list *curr;
	list *head;

	char * errorcode = NULL;
	head = NULL;
	if ( file )
		{
			char line [ BUFSIZ ];
			while ( fgets(line, sizeof line, file) )
			{
				curr = (list *)malloc(sizeof(list));
				strcpy(curr->dir, line);
				curr->next  = head;
				head = curr;
			}


			while(head) {
				printf("%s\n", curr->dir);
				head = head->next ;
			}
		}
		else
		{
		perror(filename);
		}
	}
	return head;

}




Everything works fine except for the function return type. Changing the return type to int and returning a random integer fixes this. I am looking for a way for this function to return the linked list.

Thanks.

Eclipse build error at /*Error line*/ : conflicting types for 'parse_file'
OS : Ubuntu 11.10
Eclipse Version: 3.7.0

[Updated on: Mon, 28 November 2011 11:19]

Report message to a moderator

Re: Returning structure reference in a C function [message #759566 is a reply to message #759319] Tue, 29 November 2011 08:40 Go to previous messageGo to next message
Axel Mueller is currently offline Axel MuellerFriend
Messages: 1973
Registered: July 2009
Senior Member
Your code is wrong. In C (not C++ !) you have to declare the prototype of a function before you use it.
Add the following before your main() routine
list * parse_file();


Alternatively, you could move the main() routine at the end of the file.


Before you ask
- search this forum
- see the FAQ http://wiki.eclipse.org/CDT/User/FAQ
- google
Re: Returning structure reference in a C function [message #760353 is a reply to message #759566] Fri, 02 December 2011 05:43 Go to previous message
Shashank  is currently offline Shashank Friend
Messages: 6
Registered: January 2011
Junior Member
Thanks Alex. Everything is going on fine now!
Previous Topic:!!!FATAL ERROR!!!: No such file or directory??
Next Topic:How do I see the step before termination in debug mode?
Goto Forum:
  


Current Time: Thu Apr 25 15:02:36 GMT 2024

Powered by FUDForum. Page generated in 0.03013 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top