- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用它作为一个函数的指针,该函数接受一个结构体 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 ,©Int);
Node head4 = concatLists( head1, head2, &IsPrime ,©Int);
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, ©Int);
printList(head3, "head3", &printInt);
return 0;
}
注释:
为了使调试更简单,我从 insert()
派生了 insert1()
。有一次,我第一次运行了,并且很容易将修复应用到第二次。
我添加了一个“遍历”来查找每个插入的列表末尾。 (OP 中缺少这一点,可能是有意的,也可能不是。)
我添加了printList()
函数。当我可以“看到东西”时,这让我变得更容易。
在函数 concatLists()
中,我更改了 condFunc()
的调用。 (不幸的是,OP中缺少condition_function
的类型定义。因此,我以“相同的风格”进行了它,例如print_function
和copy_function
.)
由于函数 concatLists()
会深度复制节点(即它也复制节点的数据),我想知道为什么 insert()
会这样做改为制作平面副本(即复制节点的数据指针)。对我来说,一般来说使用深层复制看起来“更干净”,但这是一个品味问题。
恕我直言,在 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/
运行 PostgreSQL(7.4 和 8.x),我认为这是可行的,但现在我遇到了错误。 我可以单独运行查询,它工作得很好,但如果我使用 UNION 或 UNION ALL,它会抛出错误。 这个错误:
我试图为我的应用程序创建一个导航,使用抽屉导航我的 fragment 之一(HomeFragment)有一个 ViewPager,可容纳 3 个 fragment (Bundy Clock、Annou
以我目前正在开发的应用为例: - 它有一个包含多个项目的抽屉导航;现在有两个项目让我感兴趣,我将它们称为 X 和 Y。 X 和 Y 都在单击时显示包含 x 元素或 y 元素列表的 fragment 选
我有一个形状为 (370,275,210) 的 NumPy 数组,我想将其重新整形为 (275,210,370)。我将如何在 Python 中实现这一点? 370是波段数,275是行数,210是图像包
我们如何与被子 UIViewController 阻止的父 UIViewController(具有按钮)交互。显然,触摸事件不会通过子 Nib 。 (启用用户交互) 注意:我正在加载默认和自定义 NI
我是 Jpa 新手,我想执行过程 我的代码如下 private static final String PERSISTENCE_UNIT_NAME = "todos"; private static
与安装了 LAMP 的 GCE 相比,选择与 Google Cloud SQL 链接的 GCE 实例有哪些优势? 我确定 GCE 是可扩展的,但是安装在其上的 mysql 数据库的可扩展性如何? 使用
这个问题在这里已经有了答案: Value receiver vs. pointer receiver (3 个答案) 关闭 3 年前。 我刚接触 golang。只是想了解为 Calc 类型声明的两种
我不小心按了一个快捷键,一个非常漂亮的断线出现在日期上。 有点像 # 23 Jun 2010 -------------------- 有人知道有问题的快捷方式吗?? (我在 mac 上工作!) 在
我正在Scala中编写正则表达式 val regex = "^foo.*$".r 这很好,但是如果我想做 var x = "foo" val regex = s"""^$x.*$""".r 现在我们有
以下 XML 文档在技术上是否相同? James Dean 19 和: James Dean 19 最佳答案 这两个文档在语义上是相同的。在 X
我在对数据帧列表运行稳健的线性回归模型(使用 MASS 库中的 rlm)时遇到问题。 可重现的示例: var1 <- c(1:100) var2 <- var1*var1 df1 <- data.f
好的,我有一个自定义数字键盘,可以在标签(numberField)中将数字显示为 0.00,现在我需要它显示 $0.00。 NSString *digit = sender.currentTitle;
在基于文档的应用程序中,使用 XIB 文件,创建新窗口时其行为是: 根据最后一个事件的位置进行定位和调整大小 window 。 如果最后一个事件窗口仍然可见,则新窗口 窗口应该是级联的,这样它就不会直
我想使用参数进行查询,如下所示: SELECT * FROM MATABLE WHERE MT_ID IN (368134, 181956) 所以我考虑一下 SELECT * FROM MATABLE
我遇到一些性能问题。 我有一个大约有 200 万行的表。 CREATE TABLE [dbo].[M8]( [M8_ID] [int] IDENTITY(1,1) NOT NULL,
我在 jquery 中的按键功能遇到问题。我不知道为什么按键功能不起作用。我已经使用了正确的 key 代码。在我的函数中有 2 个代码,其中包含 2 个事件键,按一个键表示 (+) 代码 107 和(
我想显示音频波形,我得到了此代码,它需要.raw音频输入并显示音频波形,但是当我放入.3gp,.mp3音频时,我得到白噪声,有人可以帮助我如何使其按需与.3gp一起使用使用.3gp音频运行它。 Inp
我无法让 stristr 函数返回真值,我相信这是因为我的搜索中有一个 $ 字符。 当我这样做时: var_dump($nopricecart); 完整的 $nopricecart 值是 $0 ,我得
如果我有这样的循环: for(int i=0;i O(n) 次。所以do some执行了O(n)次。如果做某事是线性时间,那么代码片段的复杂度是O(n^2)。 关于algorithm - 带 If 语
我是一名优秀的程序员,十分优秀!