Thread: Having trouble returning a structure.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11

    Having trouble returning a structure.

    I posted about this project a about a week ago and the problem was resolved.

    Since then, I have continued working on this project and have run into another problem.

    Here is the error:
    error: incompatible types when assigning to type ‘struct process’ from type ‘int’

    Code:
    struct process MM_shortest;
    typedef struct process ITEMTYPE;
    
    ITEMTYPE getLeastRemainingTime(struct node * h)
    // Finds the node containing the job with the least remaining time.
    // Deletes this node and returns the job
    {
    	ITEMTYPE p = NULLPROCESS;
    	struct node * tmp = h;
    	struct node * current = h;
    	unsigned int shortestTime = current->item.timeLeft;
    	while(current!=NULL)
    	{
    		if(current->item.timeLeft <= shortestTime)
    		{
    			tmp = current;
    			shortestTime = current->item.timeLeft;
    			p = current->item;
    		}
    		current=current->prev;	// go through the list backwards so, if processes have the same timeLeft,
    					// then the process that was in the queue first will be chosen
    		if(current == h)
    		{
    			del(h, tmp);
    			return p;
    		}
    	}
    	return NULLPROCESS;
    }
    Here is how I call getLeastRemainingTime in main:
    Code:
    MM_shortest = getLeastRemaingTime( MM_head ); /// THE ERROR IS ON THIS LINE
    I am confused as to why the compiler is saying that an int is being returned.

    Thanks for any help you can offer!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you make that call before the function definition? C only knows about stuff that comes above it in the file, so if getLeaseRemainingTime is defined below it's usage, the compiler assumes it returns an integer, and could produce an error like that.

    If that's not the case, we would need to see some more complete code to understand the context.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    Here is the complete code:

    Code:
    edit
    Last edited by awr7126; 03-28-2011 at 06:39 PM. Reason: Sorry, but this is an assignment for class and I would rather others in the class not to see my code

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <malloc.h>
    You should just be using stdlib.h here instead.
    Code:
    struct process {
    	char 		name[5];
    	char 		type[4];
    ...
    	NULLPROCESS.name[0]	= 'N','U','L','L';
    	NULLPROCESS.type[0]	= 'N','U','L','L';
    
    While this is technically legal, you go about using type as a string, which is is not with that assignment.
    Code:
    ITEMTYPE getLeastRemainingTime(struct node * h)
    // Finds the node containing the process with the least remaining time.
    // Deletes this node and returns the process
    {
    	ITEMTYPE p = NULLPROCESS;
    ...
    			return p;
    		}
    	}
    	return NULLPROCESS;
    }
    That may as well just be return p, since you've already assigned it the values of NULLPROCESS.

    I'm not really sure why you are returning structures by value here, and using pointers everywhere else. Also:
    Code:
    void del (struct node* h, struct node * current)
    {
    	if(current == NULL)
    ...
    	else if(current->next == NULL && current->prev == NULL) // 1 item in list, just the head
    	{
    		h = NULL;
    		free(current);
    	}
    	else
    	{
    		if (current == h)
    			h = current->prev;
    		current->next->prev = current->prev;
    		current->prev->next = current->next;
    		free(current);
    	}
    }
    Both of those assignments are lost as soon as this function ends. You can never change the value of a passed argument directly and have it keep when the function ends. You need a pointer to a pointer.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look at your declaration of the function:
    Code:
    ITEMTYPE getLeastRemainingTime(struct node * h)
    and your call to the function:
    Code:
    MM_shortest = getLeastRemaingTime( MM_head );
    Notice anything strange? Since it doesn't know what getLeastRemaingTime is, it assumes it's some function declared elsewhere that returns an int.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    PA
    Posts
    11
    Ahh; Thank you. I feel silly.


    If I could follow up on some of your suggestions:

    Code:
    #include <malloc.h>
    You should just be using stdlib.h here instead.
    It works either way. Whats the difference?


    I'm not really sure why you are returning structures by value here, and using pointers everywhere else.
    Is there a major advantage to using the pointer in this case? I got most of the linked list from an example where ITEMTYPE was a int so I just changed as little as possible. It would be an easy change. Will find using a pointer easier later on?


    Code:
    void del (struct node* h, struct node * current)
    {
    	if(current == NULL)
    ...
    	else if(current->next == NULL && current->prev == NULL) // 1 item in list, just the head
    	{
    		h = NULL;
    		free(current);
    	}
    	else
    	{
    		if (current == h)
    			h = current->prev;
    		current->next->prev = current->prev;
    		current->prev->next = current->next;
    		free(current);
    	}
    }
    Both of those assignments are lost as soon as this function ends. You can never change the value of a passed argument directly and have it keep when the function ends. You need a pointer to a pointer.
    Oh yeah, Thanks. Its confusing because you CAN do this in assembler.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by awr7126 View Post
    Ahh; Thank you. I feel silly.


    If I could follow up on some of your suggestions:

    It works either way. Whats the difference?
    From the Pelles C helpfile....

    <malloc.h> [not standard C] [2.60]

    #include the file <malloc.h> to declare functions for memory management (Microsoft compatibility).

    Note! The same functions are also declared in the standard file <stdlib.h> - use that file instead!

    The following data types are declared in the file:

    Data type Description
    size_t Integer type (unsigned) that can hold the result of the sizeof operator.
    At least in the case of Pelles C there are minor differences between the two malloc() functions with respect to the custom memory manager Pelle uses... It's not standard C and thus not guaranteed to work the same way on any other compiler.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by awr7126 View Post
    It works either way. Whats the difference?
    malloc.h is obsolete. stdlib.h is the new and improved version.

    Is there a major advantage to using the pointer in this case? I got most of the linked list from an example where ITEMTYPE was a int so I just changed as little as possible. It would be an easy change. Will find using a pointer easier later on?
    Size. A pointer is a pointer is a pointer (well, there are different sized pointers on some rare systems, but probably not on whatever you're using). It will likely be 4 or 8 bytes (depending on a 32-bit or 64-bit system). If you return a whole struct, you have to return the whole struct. If you have a big struct this is inefficient as you have to copy umpteen bytes on to the stack to be returned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help
    By fourseventwo in forum C Programming
    Replies: 4
    Last Post: 12-04-2010, 11:01 PM
  2. Trouble Understanding Structure Algorithm
    By JJohnson in forum C Programming
    Replies: 14
    Last Post: 10-19-2010, 01:27 AM
  3. Problem with function returning a structure
    By SkaxCo in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2007, 12:27 PM
  4. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM