gpt4 book ai didi

c - 如何处理函数声明中 C 参数名称(不带类型)的错误?

转载 作者:行者123 更新时间:2023-11-30 20:59:54 25 4
gpt4 key购买 nike

我使用它作为一个函数的指针,该函数接受一个结构体 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;
}

上面的代码需要两个节点并在特定条件下将它们相互连接..

UPDATE:

我在行 insert( &head1,(*(int *) a1), N1 );:

passing argument 2 of 'insert' makes pointer from integer without a cast [-Wint-conversion]

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

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

注释:

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

  2. 我添加了一个“遍历”来查找每个插入的列表末尾。 (OP 中缺少这一点,可能是有意的,也可能不是。)

  3. 我添加了printList()函数。当我可以“看到东西”时,这让我变得更容易。

  4. 在函数 concatLists() 中,我更改了 condFunc() 的调用。 (不幸的是,OP中缺少condition_function的类型定义。因此,我以“相同的风格”进行了它,例如print_functioncopy_function .)

  5. 由于函数 concatLists() 会深度复制节点(即它也复制节点的数据),我想知道为什么 insert() 会这样做改为制作平面副本(即复制节点的数据指针)。对我来说,一般来说使用深层复制看起来“更干净”,但这是一个品味问题。

  6. 恕我直言,在 typedef 中“隐藏”指针是一种不好的风格。由于指针是任何类型错误的常见来源,因此我认为始终“看到”它们是件好事。因此,定义了类型 Node:typedef 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 -> -|

$

关于c - 如何处理函数声明中 C 参数名称(不带类型)的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44622246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com