1

I've created the following linked list struct:

 struct node {
    struct data *data;
    struct node *next;
 };

My problem is that when I'm trying to create a function that returns this linked list struct, I'm getting the following error message:

"Conflicting types for 'function'".

My function looks like this :

struct list_node *function(location piece){
    struct list_node *details;
    details = malloc( sizeof(struct list_node) );
    details->next = NULL;
    details->current = NULL;
    if (global_var == WHITE_M){
            struct list_node *temp;
            temp = malloc( sizeof(struct list_node) );
            data new_data;
            *temp->data = new_data;
            temp->next = malloc( sizeof(struct list_node) );
            temp->next = details;
            details = temp;
        }
 return details;
}

Actually what I'm trying to do in my function is creating new linked list, then connecting new nodes to it, and return the linked list.

Every return phrase I've tried, and evert declaration I've tried brought me to the same error, can someone please help me ?

hercules
  • 11
  • 1
  • 3
  • 3
    Does the definition of the function match the declaration? Show the declaration. – R Sahu Jun 03 '15 at 17:51
  • 1
    Your first struct is named 'node'. Your function returns 'list_node*' – David W Jun 03 '15 at 17:52
  • 1
    Are you defining/declaring your function before you use it? Most (all?) compilers will give you an implicit function type, and then complain that the types don't match. – Degustaf Jun 03 '15 at 17:53
  • 3
    Your bracing in the function isn't correct; the function closes off before your return statement, so you might want to verify the source you've posted. You also have a `struct moves_list_node` in your `malloc` call, but then assign that pointer to a `list_node*` – David W Jun 03 '15 at 17:56
  • this is the declaration : struct list_node* function(location piece), i've confused some 'node' with ' list_node' while copying the function to here but now it seems ok. – hercules Jun 04 '15 at 09:13

3 Answers3

4

You have an extra curly brace at the end of the if statement. The return isn't in the function.

Joe
  • 2,008
  • 1
  • 17
  • 25
0

It's just a mistake i've made while copying the function over here, in my code the return state is located correctly, and i'm still having the same problem. i've corrected the problem in the original message.

hercules
  • 11
  • 1
  • 3
0

It could be that you're trying to use the function before it's declared. For instance, let's say you have:

int func1()
{
   prinft("%d\n", func2());
}

double func2()
{
    return 1.0;
}

If you did not create a function prototype for "func2", then the compiler assumes that the function has an integer return type. Of course it doesn't really, but because the compiler hasn't seen func2() before it is called, it makes an incorrect assumption about the return type. In your code, you could be calling the "function" function before you've defined it. You can remedy this with a function prototype, which is basically a way to tell the compiler what sort of functions to expect:

//Function prototypes
int func1();
double func2();

int func1()
{
   prinft("%d\n", func2());
}

double func2()
{
    return 1.0;
}

Also, see: conflicting types error when compiling c program using gcc

Community
  • 1
  • 1
Carlos Sanchez
  • 986
  • 1
  • 10
  • 17