开发者

Pointer that points to return of function. Initialization from incompatible pointer type

开发者 https://www.devze.com 2023-01-25 18:51 出处:网络
I have some trouble with pointers. I have a function that should return the node in a linked list, the function should return the pointer to a variable. I\'m confused and cannot see what I do wrong!

I have some trouble with pointers. I have a function that should return the node in a linked list, the function should return the pointer to a variable. I'm confused and cannot see what I do wrong!

data.h - header file

int add(int num);
struct message *findMessage(int num);

typedef struct list_el message;

data.c - Linked list.

    #include <stdio.h>
#include <stdlib.h>

struct list_el {
    int num;

    struct list_el *next;

};

typedef struct list_el message;
struct list_el *head, *tail;



int addNode(struct list_el *curr)
{
    if(head == NULL) {
        head = curr;

    } else {
        tail->next = curr;
    }

    curr->next = NULL;

    return 0;
}

int add(int num)
{
    message *curr;

    head = NULL;

    curr = (message*) malloc(sizeof(message));
    curr->num = num;
    addNode(curr);

    return 0;
}

message *findMessage(int num)
{
    message *curr;

    for(curr = head; curr != NULL; curr = curr->next) {
        if(curr->num == num) {
            return curr;
        }
    }
    return NULL;
}

Main.c - MAIN

开发者_开发百科#include <stdio.h>

#include "data.h"

int main(void)
{
 int num = 2;

 add(num);

 message *curr = findMessage(num);

 return 0;
}


Why not include the definition of struct list_el into the header? Otherwise main cannot work with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消