- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用链表数组编写一个简单的字典,但在调用显示函数后我一直丢失数据。
这是我的结构定义
typedef struct node{
int elem;
struct node *next;
}*L;
typedef L Dictionary[10];
这是我的展示
void display(Dictionary A)
{
int i;
for(i=0;i<10;i++){
printf("A[%d]: ",i);
while(A[i]!=NULL){
printf("%d\t",A[i]->elem);
A[i] = A[i]->next;
}
printf("\n");
}
}
解决办法是创建一个临时变量。
我试过了
Dictionary tempStruct
for(i=0;i<10;i++){
tempStruct[i] = A[i];
}
并且有效。但是有没有比这更有效的分配链表的其他方法?
tempStruct = A;
实际上不起作用,我得到 incompatible types node** to Dictionary{*node[10]}
最佳答案
您可以将显示函数中的循环更改为:
for(i=0;i<10;i++){
printf("A[%d]: ",i);
L tmp = A[i];
while(tmp!=NULL){
printf("%d\t",tmp->elem);
tmp = tmp->next;
}
printf("\n");
}
不需要复制整个数组,一个简单的临时指针在链表中导航就足够了。
旁注:对于数组的副本,您尝试使用tempStruct = A;
分配它。这不起作用的原因有两个:
在您的函数中,A
没有数组类型。 C 不支持将数组 传递给函数。当一个函数有一个数组类型的参数时,它会自动调整为指针类型,而不是传递一个数组,而是传递一个指向数组第一个元素的指针。这种效果通常表示为数组衰减为指针,这就是您将 incompatible types node** to Dictionary{*node[10]}
消息的原因。
即使 A
有 数组类型,它仍然无法工作,因为 C 不允许分配给一个大批。这有点令人惊讶,因为同样的事情也适用于 struct
。我想不出在 C 中不允许分配数组的充分理由,你应该记住你不能。当然,你可以手动完成,如果你不想分配每个元素,你可以使用函数 memcpy()
, 在 string.h
中声明:
int foo[5];
int bar[5] = {1, 2, 3, 4, 5};
// instead of foo = bar;
memcpy(foo, bar, sizeof foo);
与您的问题无关,但我很难理解这段代码。您的 typedef
的可读性是灾难性的。 永远不要将指针隐藏在typedef
之后——为了理解处理指针的代码,指针显而易见 很重要。数组类型的 typedef
至少也是有问题的。我建议使用以下代码:
typedef struct node {
int elem;
struct node *next;
} node;
// not strictly necessary, but IMHO, if you want to typedef a struct type,
// it's the least confusing option to name it the same as the struct tag.
#define DICTSIZE 10
void display(node **a) // variable names are often lowercase by convention
{
// to cope with ANY possible size, you need size_t, int might be too small
// include stddef.h or stdlib.h to use it. Of course, with 10 elements,
// int is enough.
for (size_t i = 0; i < DICTSIZE; ++i) {
printf("a[%zu]: ", i);
node *tmp = a[i];
// now it's obvious tmp is a pointer, so no need to explicitly
// write the != NULL ... (any pointer that's not NULL evaluates true)
while (tmp) {
printf("%d\t", tmp->elem);
tmp = tmp->next;
}
printf("\n");
}
}
还要注意一些添加的空格如何极大地提高代码的可读性(因此,请使用它们)。
我认为您的原始显示功能已损坏,因为它修改了它显示的内容。这不是显示 数据的函数的预期行为。如果您想进一步改进您的代码,您应该使用 const
来明确表示该函数不应修改它接收到的内容,以便编译器可以捕获错误。在上面的示例中,display
的签名最好如下所示:
void display(const node *const *a)
第一个 const
将使任何 struct node
不可变,第二个 const
(在星号之后)使数组中的指针不可变。有了这个,你还必须写
const node *tmp = a[i];
因为您不能将 const 指针分配给非常量指针。
关于C数组链表,将一个数组链表赋值给另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45654764/
你能解释一下这个作业是如何完成的吗, var fe, f = document.forms[0], h; 哪个等于哪个。 最佳答案 以上等同于 var fe; var f = document.for
据我测试,这两种方法都有效,但我不知道哪一种最好,也不知道它们之间的区别,这就是我想知道的。 以下是两种方法: window.location = 'http://www.google.com'; w
我正在处理用字符串填充的 numpy 数组。我的目标是分配给第一个数组 a 的切片,值包含在较小尺寸的第二个数组 b 中。 我想到的实现如下: import numpy as np a = np.em
在我使用过的其他语言(如 Erlang 和 Python)中,如果我正在拆分字符串并且不关心其中一个字段,我可以使用下划线占位符。我在 Perl 中试过这个: (_,$id) = split('
我认为这似乎很简单,但我对调用、应用、绑定(bind)感到困惑。等等 我有一个事件监听器 red.addEventListener("click", function() { j = 0;
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
几周前我们开始写一篇关于 Haskell 的论文,刚刚接到我们的第一个任务。我知道 SO 不喜欢家庭作业问题,所以我不会问怎么做。相反,如果有人能将我推向正确的方向,我将不胜感激。鉴于它可能不是一个特
我正在尝试为我的函数的变量根分配一个值,但似乎不起作用。我不明白这个问题。 hw7.c:155:7:警告:赋值使指针来自整数而不进行强制转换[默认启用] root = 负载(&fp, 大小); 此代码
我昨天花了大约 5 个小时来完成这个工作,并使用这个网站的帮助让代码可以工作,但我认为我这样做的方式是一种作弊方式,我使用了 scanf 命令。无论如何,我想以正确的方式解决这个问题。多谢你们!哦,代
我需要一些帮助来解决问题。 我有这个文本文件: 我将文本内容输入到字符串二维数组中,并将其转换为整数二维数组。当我转换为 int 数组时,nan 被替换为零。现在,我继续查找二维数组中每行的最大值和最
假设我有一个只能移动的类型。我们停止现有的默认提供的构造函数,但 Rvalue 引用引入了一种新的“ flavor ”,我们可以将其用于签名的移动版本: class CantCopyMe { priv
假设我有两个简单的对象,我想创建第三个对象来连接它们的属性。这非常有效: (()=>{ const a1 = {a: 2, b: 3} const b1 = {a: 100, c: 5}
我想知道我是否可以稍后在这样的代码中为 VAR 赋值 var myView: UIView func createView() { myView = UIView() { let _view =
我遇到了一些 Javascript/HTML/CSS 代码的问题。我对创建网站还很陌生,所以请多多包涵。 我最终想做的是从 javascript 中提取一个动态值并使用它对一些 div(在容器中)进行
#include class Box{ public: int x; Box(){ x=0; std::cout No move construction thanks to RV
我发现在javascript中&=运算符是按位赋值: var test=true; test&=true; //here test is an int variable javascript中是否存在
请帮助完成赋值重载函数的执行。 这是指令: 赋值运算符 (=),它将源字符串复制到目标字符串中。请注意,目标的大小需要调整为与源相同。 加法 (+) 和赋值 (=) 运算符都需要能够进行级联运算。这意
我有一个名为 SortedArrayList 的自定义结构它根据比较器对其元素进行排序,我想防止使用 operator[] 进行分配. 示例: 数组列表.h template class Array
我是 python 的新手,我看到了这种为列表赋值的形式 color= ['red' if v == 0 else 'green' for v in y] 但是如果我尝试用 3 个数字来做,例如 co
我是一名优秀的程序员,十分优秀!