我正在使用它作为一个函数的指针,该函数接受作为结构的Node并返回bool类型,但出现此错误:

parameter names (without types) in function declaration


结构是这样的:

struct node_t
{
    Element data;
    Node next;
};

typedef struct node_t* Node;

typedef void* Element;

typedef Element (*copy_function) (Element);


Node concatLists( Node head1, Node head2, condition_function ConditionF ,copy_function CopyFunction)
{
    Node head = NULL;
    Node *current = &head;

    for ( ; head1 != NULL; head1 = head1->next )
    {
        if ( ConditionF( head1 ) )
        {
            *current = malloc( sizeof(Node ) );
            ( *current )->data = CopyFunction(head1->data);
            ( *current )->next = NULL;
            current = &( *current )->next;
        }
    }

    for ( ; head2 != NULL; head2 = head2->next )
    {
        if ( ConditionF( head2 ) )
        {
            *current = malloc( sizeof( Node ) );
            ( *current )->data = CopyFunction(head2->data);
            ( *current )->next = NULL;
            current = &( *current )->next;
        }
    }

    return head;
}




void insert( Node *head, void* a[], size_t n )
{
    if ( *head != NULL ) head = &( *head )->next;

    for ( size_t i = 0; i < n; i++ )
    {
        Node tmp = malloc( sizeof( Node) );
        tmp->data = a[i];
        tmp->next = *head;
        *head = tmp;
        head = &( *head )->next;
    }
}


int main( void )
{
    Node head1 = NULL;
    Node head2 = NULL;
    int a1[] = { 1, 2, 3 };
    int a2[] = { 4, 5, 6 };
    const size_t N1 = sizeof( a1 ) / sizeof( *a1 );
    const size_t N2 = sizeof( a2 ) / sizeof( *a2 );

    insert( &head1,(*(int *) a1), N1 );//// i get error here
    insert( &head2,(*(int *) a2), N2 );

    Node head3 = concatLists( head1, head2,&odd ,&copyInt);
    Node head4 = concatLists( head1, head2, &IsPrime ,&copyInt);
return 0;
}


上面的代码接受两个节点,并在特定条件下将它们彼此连接。


  更新:


我在insert( &head1,(*(int *) a1), N1 );行中收到此错误:


  传递'insert'的参数2使指针从整数开始,而没有
  强制转换[-Wint-conversion]


ANOTHR更新:
//查找数据是否为奇数的函数

static bool odd( Node n )
{
    int* x=NULL;
     *x=(*(int *)getNodeData(n))%2; // error here
    return true;
}


我收到此警告可能是什么问题?

最佳答案

为了提供一些帮助,这是我到目前为止所获得的:

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

struct node_t {
  void *data;
  struct node_t *next;
};
typedef struct node_t *Node;

typedef void(*print_function)(void*);

typedef void* (*copy_function)(void*);

typedef int (*condition_function)(void*);

void printList(Node head, const char *label, print_function printFunc)
{
  printf("%s", label);
  for (; head != NULL; head = head->next) {
    printf(" -> "); (*printFunc)(head->data);
  }
  printf(" -> -|\n");
}

void insert1(Node *head, void *a)
{
  Node *current = head;
  // find tail pointer (i.e. the end of list)
  while (*current != NULL) {
    current = &(*current)->next;
  }
  // make node node
  *current = malloc(sizeof(Node));
  (*current)->data = a; // Attention! This is a flat copy.
  (*current)->next = NULL;
}

void insert(Node *head, void *a[], size_t n)
{
  Node *current = head;
  // find tail pointer (i.e. the end of list)
  while (*current != NULL) {
    current = &(*current)->next;
  }
  // insert nodes
  for (size_t i = 0; i < n; ++i) {
    *current = malloc(sizeof(Node));
    (*current)->data = a[i]; // Attention! This is a flat copy.
    (*current)->next = NULL;
    current = &(*current)->next;
  }
}

Node concatLists(
  Node head1, Node head2, condition_function condFunc, copy_function copyFunc)
{
  Node head = NULL;
  Node *current = &head;
  for (; head1 != NULL; head1 = head1->next) {
    if (condFunc(head1->data)) {
      *current = malloc(sizeof(Node));
      (*current)->data = copyFunc(head1->data);
      (*current)->next = NULL;
      current = &(*current)->next;
    }
  }
  for (; head2 != NULL; head2 = head2->next) {
    if (condFunc(head2->data)) {
      *current = malloc(sizeof(Node));
      (*current)->data = copyFunc(head2->data);
      (*current)->next = NULL;
      current = &(*current)->next;
    }
  }
  return head;
}

void printInt(void *data) { printf("%d", *(int*)data); }

void* copyInt(void *data)
{
  int *newData = malloc(sizeof(int));
  *newData = *(int*)data;
  return newData;
}

int isOdd(void *data)
{
#if 0 /* my style: */
  return *(int*)data & 1;
#else /* like in OP: */
  return *(int*)data % 2;
#endif /* 0 */
}

int main()
{
  Node head1 = NULL, head2 = NULL;
  int a1[] = { 1, 2, 3 };
  int a2[] = { 4, 5, 6 };
  insert1(&head1, a1);
  insert1(&head1, a1 + 1);
  insert1(&head1, &a1[2]);
  printList(head1, "head1", &printInt);
  { void *pA2[] = { a2, a2 + 1, &a2[2] };
    enum { N2 = sizeof pA2 / sizeof *pA2 };
    insert(&head2, pA2, N2);
  }
  printList(head2, "head2", &printInt);
  Node head3 = concatLists(head1, head2, &isOdd, &copyInt);
  printList(head3, "head3", &printInt);
  return 0;
}


笔记:


为了简化调试,我从insert1()派生了insert()。有一次,我运行了第一个,并且很容易将修复程序应用于第二个。
我添加了一个“遍历”以查找每个插入的列表结尾。 (这在OP中是缺失的,可能会或可能不会。)
我添加了printList()函数。当我可以“看到”时,这对我来说变得容易得多。
在功能concatLists()中,我更改了condFunc()的调用。 (不幸的是,OP中缺少condition_function的类型定义。因此,我使用的是“相同样式”,如print_functioncopy_function。)
当函数concatLists()制作节点的深层副本(即,它也复制节点的数据)时,我想知道为什么insert()却制作平面副本(即,复制节点的数据指针)。对我来说,通常使用深层复制看起来“干净”,但这是有问题的。
恕我直言,在typedef中“隐藏”指针是不好的样式。由于指针是任何类型错误的常见原因,因此我认为始终“看到”它们是很好的。
因此,我定义了类型Nodetypedef struct node_t Node;,并且始终将其与***一起使用以显式进行间接寻址。 (实际上,我首先是这样做的,但是在发布代码之前进行了更改,使其像在OP中一样。)


在cygwin中用gcc测试:

$ gcc -std=c11 -o test-list test-list.c

$ ./test-list
head1 -> 1 -> 2 -> 3 -> -|
head2 -> 4 -> 5 -> 6 -> -|
head3 -> 1 -> 3 -> 5 -> -|

$

10-07 19:52
查看更多