如何在函数声明中处理c参数名称(无类型)中的此错误?

时间:2017-06-19 04:24:28

标签: c pointers

我使用它作为指向一个函数的指针,该函数接受一个结构的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 );行中收到此错误:

  

传递&#39;插入&#39;的参数2从没有a的整数生成指针   施放[-Wint-conversion]

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

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

我得到这个警告可能是什么问题?

2 个答案:

答案 0 :(得分:0)

我无法产生确切的错误,但这是我的猜测:

insert( &head1,(*(int *) a1), N1 );

在这一行以及随后的下一行中。

第二个参数的格式为:(*(int *) a1)

让我为你解释一下:

a1的类型为int *

*(取消引用运算符)和(int *)(类型转换运算符)具有相同的优先级。从右到左是联想的。

因此,这将会发生:

  1. a将被输入int *(已经存在)
  2. 然后执行*a(地址a的值)。返回int,即a[0]
  3. 但函数需要一个指针,而不是一个int。因此它会抛出错误。
  4. 注意:您的代码有很多错误,这使得我很难验证答案的正确性。请提供在任何C编译器(或至少gcc)上编译时会产生与您解释相同的错误的确切代码。

答案 1 :(得分:0)

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

#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;
}

注意:

  1. 为了简化调试,我从insert1()派生了insert()。有一次,我第一次运行并很容易将修复程序应用到第二个。

  2. 我添加了一个&#34;遍历&#34;找到每个插入的列表的结尾。 (这在OP中缺失,可能会或可能不会。)

  3. 我添加了printList()功能。这使得我更容易看到#34;

  4. 在功能concatLists()中,我更改了condFunc()的调用。 (不幸的是,OP中缺少condition_function的类型定义。因此,我在&#34;相同的样式&#34;如print_functioncopy_function中完成了。)

  5. 由于函数concatLists()制作了节点的深层副本(即它也复制了节点的数据),我想知道为什么insert()会复制平面副本(即复制数据< em>指针节点)。对我来说,它看起来更清洁&#34;一般情况下使用深层复制品,但这只是一种品味问题。

  6. 恕我直言,它的风格很糟糕&#34;隐藏&#34; typedef中的指针。由于指针是任何类型错误的常见来源,我认为&#34;看到&#34;他们总是。 因此, I 已定义类型Nodetypedef struct node_t Node;,并始终使用***来明确制定间接。 (实际上,我是先这样做的,但是在发布代码之前改变了它,就像在OP中完成它一样。)

  7. 在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 -> -|
    
    $
    
相关问题